Issue with DependencyProperty binding

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

I created a small File Browser Control:



        
3条回答
  •  你的背包
    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:

    
    

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

    
    

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

提交回复
热议问题