Issue with DependencyProperty binding

后端 未结 3 589
渐次进展
渐次进展 2020-11-21 16:07

I created a small File Browser Control:



        
相关标签:
3条回答
  • 2020-11-21 16:43

    Do not ever set DataContext of UserControl inside usercontrol:

    THIS IS WRONG:

    this.DataContext = someDataContext;
    

    because if somebody will use your usercontrol, its common practice to set its datacontext and it is in conflict with what you have set previously

    <my:SomeUserControls DataContext="{Binding SomeDataContext}" />
    

    Which one will be used? Well, it depends...

    The same applies to Name property. you should not set name to UserControl like this:

    <UserControl x:Class="WpfApplication1.SomeUserControl" Name="MyUserControl1" />
    

    because it is in conflict with

    <my:SomeUserControls Name="SomeOtherName" />
    

    SOLUTION:
    In your control, just use RelativeSource Mode=FindAncestor:

    <TextBox Text="{Binding SelectedFile, RelativeSource={RelativeSource AncestorType="userControls:FileBrowserControl"}" />
    

    To your question on how are all those third party controls done: They use TemplateBinding. But TemplateBinding can be used only in ControlTemplate. http://www.codeproject.com/Tips/599954/WPF-TemplateBinding-with-ControlTemplate

    In usercontrol the xaml represents Content of UserControl, not ControlTemplate/

    0 讨论(0)
  • 2020-11-21 16:44

    The primary problem is that you set your UserControl's DataContext to itself in its constructor:

    DataContext = this;
    

    You should not do that, because it breaks any DataContext based Bindings, e.g. to a view model instance in the inherited DataContext value.

    Instead you would change the binding in the UserControl's XAML like this:

    <TextBox Text="{Binding SelectedFile,
                    RelativeSource={RelativeSource AncestorType=UserControl}}" />
    

    Now, when you use your UserControl and write a binding like

    <userControls:FileBrowserControl SelectedFile="{Binding SelectedFile}" />
    

    the SelectedFile property gets bound to a SelectedFile property in your view model, which should be in the DataContext inherited from a parent control.

    0 讨论(0)
  • 2020-11-21 16:57

    Using this:

    <userControls:FileBrowserControl SelectedFile="{Binding SelectedFile}" ...
    

    The FileBrowserControl's DataContext has already been set to itself, therefore you are effectively asking to bind to the SelectedFile where the DataContext is the FileBrowserControl, not the parent ViewModel.

    Give your View a name and use an ElementName binding instead.

    SelectedFile="{Binding DataContext.SelectedFile, ElementName=element}"
    
    0 讨论(0)
提交回复
热议问题