我在WPF应用程序中使用了DataTemplate
-
<DataTemplate x:Key="mattersTemplate">
<Border Name="border" BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="FileRef:"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FileRef}" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Description:"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Description}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Priority:"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Priority}"/>
</Grid>
</Border>
</DataTemplate>
然后我(在DocumentSetTemplateSelector
类中)定义要使用的模板;
我想做/知道的是; 创建4个其他模板,这些模板将继承上述模板,然后允许覆盖某些属性;
一个例子(这个模板继承自上面的类) - 所以它们看起来一样;
<DataTemplate x:Key="documentSet_Accounting">
<ContentPresenter Content="{Binding mattersTemplate}"
ContentTemplate="{StaticResource mattersTemplate}">
</ContentPresenter>
</DataTemplate>
我希望有一个风格附加到这个(如果可能的话),以便获得这种效果;
<DataTemplate x:Key="documentSet_Accounting">
<ContentPresenter fontsize="20" Content="{Binding mattersTemplate}"
ContentTemplate="{StaticResource mattersTemplate}">
</ContentPresenter>
</DataTemplate>
或
<DataTemplate x:Key="documentSet_Accounting">
<ContentPresenter Style="AccountingStyle" Content="{Binding mattersTemplate}"
ContentTemplate="{StaticResource mattersTemplate}">
</ContentPresenter>
</DataTemplate>
答案 0 :(得分:1)
如何在模板中使用样式继承而不是模板继承?
<Style x:Key="mattersTemplateStyle">
<Setter Property="TextBlock.Foreground" Value="Green"/>
</Style>
<Style x:Key="documentSet_AccountingStyle" BasedOn="{StaticResource mattersTemplateStyle}">
<Setter Property="TextBlock.FontSize" Value="20"/>
</Style>
<DataTemplate x:Key="mattersTemplate">
<Border Name="border" BorderBrush="Aqua" BorderThickness="1" Padding="5" Margin="5">
<Grid Style="{StaticResource mattersTemplateStyle}">
[...]
</Grid>
</Border>
</DataTemplate>
<DataTemplate x:Key="documentSet_Accounting">
<Grid Style="{StaticResource documentSet_AccountingStyle}">
<ContentPresenter Content="{Binding mattersTemplate}" ContentTemplate="{StaticResource mattersTemplate}"></ContentPresenter>
</Grid>
</DataTemplate>