我可以将现有数据模板添加到控件的资源中吗?

时间:2011-08-30 13:18:08

标签: wpf xaml controls decimal datatemplate

我目前有一个应用于单元格的数据模板,以便以某种方式设置样式:

<DataTemplate x:Key="percentageCellContentTemplate4">
    <TextBlock  VerticalAlignment="Center" Height="17" TextAlignment="Right" Margin="-1,-1,-1,-1" Background="White" Text="{Binding Converter={StaticResource PercentScale4}, ConverterParameter=' \{0:P\}'}" ToolTip="{Binding}"/>
</DataTemplate>

现在我有一个案例需要将其添加为数据网格资源中的类型模板。目前,这是有效的:

<xcdg:DataGridControl.Resources>
     <ResourceDictionary>
          <DataTemplate DataType="{x:Type s:Decimal}">
              <!-- This is exactly percentageCellContentTemplate4 - if someone can find a way to inherit this dataTemplate, free poutine. -->
              <TextBlock  VerticalAlignment="Center" Height="17" TextAlignment="Right" Margin="-1,-1,-1,-1" Background="White" Text="{Binding Converter={StaticResource PercentScale4}, ConverterParameter=' \{0:P\}'}" ToolTip="{Binding}"/>
          </DataTemplate>
     </ResourceDictionary>
</xcdg:DataGridControl.Resources>

任何人都可以想到一种方法,我避免复制粘贴完全相同的模板在这里以及我需要它的类似地方吗?

由于

2 个答案:

答案 0 :(得分:3)

这样的事情怎么样:

<DataTemplate DataType="{x:Type s:Decimal}">
    <ContentPresenter ContentTemplate="{StaticResource percentageCellContentTemplate4}" />
</DataTemplate>

修改:对其进行测试,似乎有效。

答案 1 :(得分:0)

虽然我认为H.B.的答案应该有效,但另外还有两种可能性(现在没有WPF)可以解决这个问题:

  • 将您在TextBox上设置的所有内容解压缩到可以在两种情况下应用的样式
  • 将文本块本身定义为资源,并在两个模板中引用它