本文介绍了样式不适用于DataTemplates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为资源字典中的两个类创建DatTemplates。我希望能够将统一样式(此处为TextBlock)应用于DataTemplates。但是当我运行应用程序时,我没有看到应用的样式。如果我将样式放在每个datatemplates中,那么我会得到密钥重复错误。我如何做到这一点。
I am creating DatTemplates for two classes in a resource dictionary. I want to be able to apply a uniform style (here for TextBlock) to both the DataTemplates. But when I run the application I do not see the style applied. If I put the styles in each of the datatemplates then I get key duplication error. How do I accomplish this.
<ResourceDictionary xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:my="clr-namespace:TestDataTemplateResources"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary> <Style TargetType="TextBlock"> <Setter Property="Background" Value="Yellow"/> <Setter Property="FontStyle" Value="Italic"/> </Style> </ResourceDictionary> <ResourceDictionary> <DataTemplate DataType="{x:Type my:PersonClass}"> <Border BorderBrush="Blue" BorderThickness="2"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding Name}"/> <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding Age}"/> </Grid> </Border> </DataTemplate> <DataTemplate DataType="{x:Type my:AddressClass}"> <Border BorderBrush="Blue" BorderThickness="2"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding Street}"/> <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding Zip}"/> </Grid> </Border> </DataTemplate> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>推荐答案
尝试删除MergedDictionaries。只需将样式和两个数据模板放在根ResourceDictionary中。或者至少在同一个单独的合并字典中。 try to remove MergedDictionaries at all. Just place style and both data templates in the root ResourceDictionary. Or at least in the same single merged dictionary.
嗨Irina,我尝试了你的建议并将所有内容放在root resourcedictionary中。但是数据模板仍然没有采用这种风格。 Hi Irina, I tried your suggestion and placed everything in the root resourcedictionary. But still the datatemplates do not pick up the style. <resourcedictionary xmlns="schemas.microsoft/winfx/2006/xaml/presentation"> xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:my="clr-namespace:TestDataTemplateResources"> <style targettype="TextBlock"> <setter property="Background" value="Yellow" /> <setter property="FontStyle" value="Italic" /> </style> <datatemplate datatype="{x:Type my:PersonClass}"> <border borderbrush="Blue" borderthickness="2"> <grid> <grid.columndefinitions> <columndefinition width="Auto" /> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="Auto" /> <rowdefinition height="Auto" /> </grid.rowdefinitions> <textblock grid.column="0" grid.row="0" text="{Binding Name}" /> <textblock grid.column="0" grid.row="1" text="{Binding Age}" /> </grid> </border> </datatemplate> <datatemplate datatype="{x:Type my:AddressClass}"> <border borderbrush="Blue" borderthickness="2"> <grid> <grid.columndefinitions> <columndefinition width="Auto" /> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="Auto" /> <rowdefinition height="Auto" /> </grid.rowdefinitions> <textblock grid.column="0" grid.row="0" text="{Binding Street}" /> <textblock grid.column="0" grid.row="1" text="{Binding Zip}" /> </grid> </border> </datatemplate> </resourcedictionary>
更多推荐
样式不适用于DataTemplates
发布评论