Can I add an existing data template to a control's resources?
I currently have a data template I apply to cells to get them styled a certain way:
<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>
Now I have a case where I ne开发者_Python百科ed to add this as a typed template within a datagrid's resources. Currently, this is working:
<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>
Can anyone think of a way that I avoid copy pasting the exact same template here and similar places where I need it?
Thanks
How about something like this:
<DataTemplate DataType="{x:Type s:Decimal}">
<ContentPresenter ContentTemplate="{StaticResource percentageCellContentTemplate4}" />
</DataTemplate>
Edit: Tested it and it seems to work.
While I think H.B.'s answer should work, two other possibilities come to mind (don't have WPF at hand right now) which can work out:
- extract all things you set on the TextBox to a style which you can then apply in both situations
- define the textblock itself as a resource and reference it in the two templates
精彩评论