2
我有一個資源字典,它是一個datagrid風格定義在它和datagrid風格內的風格。如何使用資源字典中的<Styles.Resources>內定義的資源
<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
而不將其移動到資源字典根目錄?
在此先感謝。
我已經在我的xaml中包含了我的resourcedictionary。 – 2012-03-19 05:02:49
x:Key =「RightAlignedDataGridCell」。你需要刪除這個我認爲。 – NVM 2012-03-19 07:48:00
不能這樣做,因爲列將左對齊。 – 2012-03-20 14:21:00