开发者

How to have classes of DataTemplates?

In my application, I would like to have DataTemplates such that I can say:

  • These are the DataTemplates for use in a TreeView
  • These are the DataTemplates to use when showing the summary of an object
  • These are the DataTemplates to use when showing details

The only way I've seen to be able to do this is to create a DataTemplateSelector and manually return the DataTemplate (possibly by a naming convention) for the class I need. Is there开发者_开发知识库 any more elegant way of handling this situation?


I'm not sure I completely understand what you're looking for, but you should just be able to define the DataTemplates in a ResourceDictionary at the scope that most makes sense for you (Application, Window, Element or external) with an x:Key of the data type they're meant for and they will automatically be selected by WPF's intrinsic data template selection engine.

For example:

<Window ...>
    <Window.Resources>
        <DataTemplate x:Key="{x:Type myns:MyDataType}">
           <!-- your template definition here -->
        </DataTemplate>
    </Window.Resources>
</Window>

Now wherever an instance of MyDataType is encountered within that Window WPF will automatically select that template to display its data.


you can set DataType property of DataTemplate. At runtime template will automatically assigned to object of its Type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜