开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜