Difference between Control Template and DataTemplate in WPF

前端 未结 7 1516
孤城傲影
孤城傲影 2020-12-07 07:13

What is difference between a ControlTemplate and a DataTemplate in WPF?

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 07:51

    All of the above answers are great but there is a key difference that was missed. That helps make better decisions about when to use what. It is ItemTemplate property:

    • DataTemplate is used for elements that provide ItemTemplate property for you to replace its items' content using DataTemplates you define previously according to bound data through a selector that you provide.

    • But if your control does not provide this luxury for you then you still can use a ContentView that can display its content from predefined ControlTemplate. Interestingly, you can change the ControlTemplate property of your ContentView at runtime. One more thing to note that unlike controls with ItemTemplate property, you cannot have a TemplateSelector for this (ContentView) control. However, you still can create triggers to change the ControlTemplate at runtime.

提交回复
热议问题