ControlTemplate with DataTrigger Vs. DataTemplate with DataTemplateSelector

后端 未结 4 1255
孤城傲影
孤城傲影 2020-12-08 23:10

I have a generic control which displays an editor based on the type property inside a ViewModel. Currently it\'s implemented using Control, ControlTemplat

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 00:03

    I've heard that DataTemplateSelectors do not update the template if the value they're based on changes, and because of this I usually don't use them.

    My preferred method is actually to use DataTemplates.

    
        
            
        
        
            
        
        ...
    
    

    Second, if I want to change the template based on a property rather than the object type, I tend to use DataTriggers. This is because if that property ever gets changed, the PropertyChange notification will automatically tell the UI that it has changed and to update the template. I do not believe DataTemplateSelectors do this automatically. I also prefer to see the template selection logic in my XAML, not have it hidden in a TemplateSelector file, but that's just personal preference.

    And my last choice is to use a DataTemplateSelector. I almost never use one in a WPF application, although I often do in Silverlight since it doesn't support my preferred method of using implicit DataTemplates (yet)

    I am not aware of any significant performance differences between the two, although I would be interested if someone can tell me otherwise.

提交回复
热议问题