Ste*_*ana 5 wpf xaml resourcedictionary
我有一个资源字典,它是在其上定义的数据网格样式以及该数据网格样式内的样式。
<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>
Run Code Online (Sandbox Code Playgroud)
然后在我的 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>
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我收到资源未找到异常。如果我将该样式放在资源字典根上。它可以工作。但我想RightAlignedDataGridCell留在
<Style.Resources>里面CatalogDataGrid。
如何RightAlignedDataGridCell在我的 XAML 上使用它而不将其移动到资源字典根目录?
提前致谢。
您必须将资源字典包含在您使用的任何控件/窗口等的资源部分中才能找到它。您可以通过MergedDictionaries来完成此操作。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myresourcedictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17660 次 |
| 最近记录: |