У меня есть словарь ресурсов, в котором определен стиль сетки данных и стиль внутри этого стиля сетки данных.
<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
.
Как использовать это RightAlignedDataGridCell
в моем XAML, не перемещая его в корень словаря ресурсов?
Заранее спасибо.