gpt4 book ai didi

wpf - 数据模板的复用

转载 作者:行者123 更新时间:2023-12-04 18:52:41 25 4
gpt4 key购买 nike

我的 Treelistview 控件中有两个 DataTemplates 用于不同的单元格。
模板是 90% 相似的。
如何重用相同的 Xaml ?

  <DataTemplate x:Key="@names">
<TextBlock Name="txt" Text="{Binding Names}" Style="{DynamicResource @BasicTextBlockStyle}"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="True">
<Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
</DataTrigger>
<DataTrigger Binding="{Binding IsScoped}" Value="True">
<Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>

<DataTemplate x:Key="@Ages">
<TextBlock Name="txt" Text="{Binding Age}" Style="{DynamicResource @BasicTextBlockStyle}"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="True">
<Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
</DataTrigger>
<DataTrigger Binding="{Binding IsScoped}" Value="True">
<Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>

最佳答案

你可以尝试做这样的事情:

<DataTemplate x:Key="Inner">
<TextBlock Name="txt" Text="{Binding}" Style="{DynamicResource @BasicTextBlockStyle}"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border},AncestorLevel=1}, Path=BorderBrush,Mode=OneWay}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="True">
<Setter Property="Style" Value="{DynamicResource @HeaderTextBlockStyle}" TargetName="txt" />
</DataTrigger>
<DataTrigger Binding="{Binding IsScoped}" Value="True">
<Setter Property="Style" Value="{DynamicResource @RootElementTextBlockStyle}" TargetName="txt" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>

<DataTemplate x:Key="Names">
<Border BorderBrush="Black" BorderThickness="1" Height="100" Width="100" HorizontalAlignment="Left">
<ContentPresenter Content="{Binding Names}" ContentTemplate="{StaticResource Inner}" />
</Border>
</DataTemplate>

<DataTemplate x:Key="Ages">
<Border BorderBrush="Black" BorderThickness="1" Height="100" Width="100" HorizontalAlignment="Right">
<ContentPresenter Content="{Binding Ages}" ContentTemplate="{StaticResource Inner}" />
</Border>
</DataTemplate>

关于wpf - 数据模板的复用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3958984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com