(First: I have some programming experience but am a beginner with XAML/WPF/StackOverflow. So please forgive anything "stupid" and ask if anything is not clearly described or you need additional info. Thanks.)

Introduction: I have a base class Item with some properties (like Title, Notes, etc.). From that I have some derived classes like ContactItem, MediaItem, etc. with additional properties which act as base classes for further specialized item types (e.g. ImageItem, MusicItem and VideoItem which are derived from MediaItem; Person and Institution are derived from ContactItem).

In WPF I want a page where multiple item types can be displayed together. I currently use an ItemsPanel for this and started to specify data templates for each item type - and there are many (more than 50).

Problem: What I now want is some kind of "inheritance" of the item controls (e.g. have a "base" UserControl with a ContentPresenter and add additional controls for additional properties of derived classes/control templates).

What would be the best way to handle this in WPF/XAML without having to copy/paste the controls from the base classes for the derived item types in the data templates?

Any idea or hint in the right direction would be great. If you need some code or additional info, please let me know.

有帮助吗?

解决方案

You could simply nest each base class UserControl inside more derived UserControls, but you could find that this might soon become unmanageable. Another way would be to use the ContentControl element to display sections of larger DataTemplates from other DataTemplates like this:

<DataTemplate x:Key="NameTemplate" DataType="{x:Type ViewModels:UserViewModel}">
    <TextBlock Text="{Binding Name}" />
</DataTemplate>
...
<DataTemplate DataType="{x:Type ViewModels:UserDerivedViewModel}">
    <ContentControl Content="{Binding}" 
        ContentTemplate="{StaticResource NameTemplate}" />
    <TextBlock Text="{Binding Age}" />
</DataTemplate>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top