In WPF, you can create one DataTemplate on another, how can you, using style?

Possible duplicate:
Dataset Inheritance

I have several data types that are not subclasses, and they do not have a common interface, but they have common properties that I want to display in the XAML DataTemplate. However, I know that this is possible ...

<!-- Basic Style Inheritance --> <Style x:Key="FooStyle" TargetType="Foo" /> <Style x:Key="EnhancedFooStyle" TargetType="Foo" BasedOn="{StaticResource FooStyle}" /> <!-- Inheritance By Type --> <Style x:Key="BaseItemStyle"> <Setter Property="Control.Background" Value="Yellow" /> </Style> <!-- These three data types share the same 'BaseItemStyle' --> <Style TargetType="ListBoxItem" BasedOn="{StaticResource BaseItemStyle}" /> <Style TargetType="ComboBoxItem" BasedOn="{StaticResource BaseItemStyle}" /> <Style TargetType="TreeViewItem" BasedOn="{StaticResource BaseItemStyle}" /> 

But can we do something similar for data templates that do not have the BasedOn property?

 <DataTemplate x:Key="CommonTemplate"> <!-- Common Stuff Goes Here --> </DataTemplate> <!-- These three datatypes share the same DataTemplate --> <DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}" /> <DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" /> <DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" /> 

I know that BasedOn not what we want here, because it is not “founded”, but rather “in” in this scenario, but does not know how to do this in XAML. As I write this, I have an idea, but I feel that using UserControl changing ...

 <UserControl x:Key="CommonTemplate" x:Shared="False"> <!-- Common Stuff Goes Here --> </UserControl> <!-- These three datatypes share the same DataTemplate --> <DataTemplate DataType="Foo1" BasedOn="{StaticResource CommonTemplate}"> <StaticResource ResourceKey="CommonTemplate" /> </DataTemplate> <DataTemplate DataType="Foo2" BasedOn="{StaticResource CommonTemplate}" /> <StaticResource ResourceKey="CommonTemplate" /> </DataTemplate> <DataTemplate DataType="Foo3" BasedOn="{StaticResource CommonTemplate}" /> <StaticResource ResourceKey="CommonTemplate" /> </DataTemplate> 

Thanks!

+6
source share
2 answers

Finding SO before publishing is your friend;)

Is there a way to use data template inheritance in WPF?

+4
source

@Foovanadil, actually I think I came up with something better. My new approach not only avoids additional binding (the one that is present in the content presenter), but also eliminates the need to use the template in general by this host, since you explicitly set its contents. Both of these things should speed up your user interface, especially in larger and more complex interfaces.

 <Border x:Shared="False" x:Key="Foo" BorderBrush="Red" BorderThickness="1" CornerRadius="4"> <TextBlock Text="{Binding SomeProp}" /> </Border> <DataTemplate x:Key="TemplateA"> <ContentPresenter Content="{StaticResource Foo}" /> </DataTemplate> <DataTemplate x:Key="TemplateB"> <ContentPresenter Content="{StaticResource Foo}" /> </DataTemplate> 

Important. Be sure to use the x:Shared attribute in your shared content, otherwise it will not work.

M

+5
source

Source: https://habr.com/ru/post/887493/


All Articles