Issue with DependencyProperty binding

后端 未结 3 645
渐次进展
渐次进展 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

    
    

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

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

    
    

    because it is in conflict with

    
    

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

    
    

    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/

提交回复
热议问题