继承/基于数据模板

4

我有一个 DataTemplate,在我的 WPF 应用程序中使用 -

<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>

可能是在WPF中使用数据模板继承的方法的重复问题。 - Arsen Mkrtchyan
问题在于我不确定如何向documentSet_Accounting模板添加“样式”(以便该模板内的字体大小等内容不同)...(如果我没有表达清楚,敬请谅解)。 - Hexie
嗨,ArsenMkrt 我看到了那篇帖子,它主要是关于如何将一个数据模板与另一个链接起来,没有提到如何向继承模板添加样式。 - Hexie
没有继承,只有组合,正如链接所解释的那样。 - hansmaad
基本上,我想为我的documentSet_Accounting模板添加一个样式,以影响ContentTemplate="{StaticResource mattersTemplate}"的外观(即这样我就可以在所有datatemplates上使用此ContentTemplate="{StaticResource mattersTemplate}",然后根据需要单独调整(例如)字体大小)。 - Hexie
如果您想修改模板,可以在其中放置一个新的ContentControl,并通过View Model为其提供模板。或者,您可以使用附加属性来修改模板内部的属性。您还可以使附加属性继承,这意味着您可以在树的更高层设置属性,以便在模板内的某个地方使用它。 - dowhilefor
1个回答

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>

嗨,史蒂芬, 看起来你走在了正确的道路上 - 谢谢。 我正在寻找的是同样的东西,但我想更改网格中的单个属性,而不是为整个网格设置样式。 例如, 此网格当前加载5个条目(来自静态类),其中一个文本框包含名为DocumentSet的字段,我正在尝试使所有值符合相同的网格外观,但根据DocumentSet值(即“会计”)仅更改字体颜色。 - Hexie
嗨@Hexie,很抱歉这么久没联系了,你还在关注这个问题吗?我不确定在documentSet_Accounting模板中设置网格样式与直接在内容呈现器上设置字体大小之间的区别是什么。我认为这两个操作都会做同样的事情?在我的上面的例子中,所有您的documentSet显示都具有绿色前景颜色(这是“基础”样式的一部分),而只有值为“会计”的documentSet才会具有20号字体大小,因为它在“派生”样式中。 - Stephen Holt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接