开发者

Is there a way for two DataTemplates to share some elements?

开发者 https://www.devze.com 2023-01-15 11:36 出处:网络
Something开发者_如何学C like a template or base DataTemplate which both DataTemplates extend or inherit so I don\'t have to duplicate XAML.What about UserControls? Create a base UserControl and then e

Something开发者_如何学C like a template or base DataTemplate which both DataTemplates extend or inherit so I don't have to duplicate XAML.


What about UserControls? Create a base UserControl and then extend the second one?

<DataTemplate>
  <local:MyBase />
</DataTemplate>

And extend it like this?

<DataTemplate>
  <local:MyBase />
  <local:SomeOtherStuff />
</DateTemplate>


You can nest DataTemplates. Here an is example

<DataTemplate x:Key="InnerTemplate">
    <TextBlock Text="{Binding}" Foreground="Purple" />
</DataTemplate>

<DataTemplate x:Key="OuterTemplate">
    <StackPanel>
        <TextBlock Text="Header" Foreground="Red" />
        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource InnerTemplate}" />
    </StackPanel>
</DataTemplate>

In this case I just have a List bound to a listbox, and its itemtemplate set to the OuterTemplate template.

<ListBox x:Name="_lbTest" Grid.Row="1" ItemTemplate="{StaticResource OuterTemplate}" ></ListBox>
0

精彩评论

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