【发布时间】:2012-03-17 05:53:37
【问题描述】:
我有一个资源字典,它是在其上定义的数据网格样式和该数据网格样式中的样式。
<Style TargetType="{x:Type DataGrid}" x:Key="CatalogDataGrid">
<Style.Resources>
<Style TargetType="{x:Type DataGridCell}" x:Key="RightAlignedDataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border
Padding="5,0"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ContentPresenter
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Center"
HorizontalAlignment="Right"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
然后在我的 XAML 中我尝试使用 RightAlignedDataGridCell 以便我的列右对齐。
<DataGrid... Style="{StaticResource CatalogDataGrid}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}">
</DataGridTextColumn>
<DataGridTextColumn Header="Total" Binding="{Binding Total}"
CellStyle="{StaticResource RightAlignedDataGridCell}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
当我运行我的应用程序时,我收到了资源未找到异常。如果我把这种风格放在资源字典根目录上。它可以工作。但我希望RightAlignedDataGridCell 留在里面
<Style.Resources> 的 CatalogDataGrid。
如何在我的 XAML 上使用 RightAlignedDataGridCell 而不将其移动到资源字典根目录?
提前致谢。
【问题讨论】:
标签: wpf xaml resourcedictionary