【问题标题】:Inheriting / BasedOn datatemplate继承/基于数据模板
【发布时间】:2012-11-16 05:43:29
【问题描述】:

我有一个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>

【问题讨论】:

  • 问题是我不确定如何向 documentSet_Accounting 模板添加“样式”(例如,该模板中的字体大小)会有所不同......(如果我没问清楚。
  • 嗨 ArsenMkrt 我看过那篇文章,这都是关于如何将一个 Datatemplate 与另一个链接,没有什么是如何向继承模板添加样式的。
  • 没有继承。只有链接解释的成分..
  • 基本上,我想在我的 documentSet_Accounting 模板中添加一个样式,该样式会影响 ContentTemplate="{StaticResource thingsTemplate} 的外观(即这样我就可以使用这个 ContentTemplate="{StaticResource thingsTemplate } 在我所有的数据模板上,然后根据需要单独调整(例如)字体大小)

标签: wpf datatemplate basedon


【解决方案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 值(即“ Accounting") 仅更改字体颜色。
  • 嗨@Hexie 很抱歉已经有一段时间了,你还在看这个问题吗?我不确定在 documentSet_Accounting 模板内的 Grid 上设置样式与直接在内容演示者上设置字体大小之间有什么区别。我原以为这两个操作会做同样的事情?在我上面的示例中,all 您的 documentSet 显示具有绿色前景色(这是“基本”样式的一部分),而只有值为“Accounting”的 documentSet 的字体大小为 20那是“派生”的风格。
猜你喜欢
  • 2014-04-07
  • 1970-01-01
  • 2012-01-10
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 2016-12-03
相关资源
最近更新 更多