在 WPF 中将 ResourceDictionary 与其他样式一起使用
Use ResourceDictionary with other Styles in WPF
我正在尝试在我的 WPF 程序中使用 ResourceDictionary 和 Style。当我在 <Window.Resources>
中只有 ResourceDictionary 时,一切正常,但只要我添加 <Style>
,程序就会为字典显示 "Resource not found",我得到一个错误 "The resource "PlusMinusExpander" could没有得到解决。"
<Window.Resources>
<Style x:Key="CurrencyCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="#dddddd" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
<ResourceDictionary x:Key="hello" Source="Assets/PlusMinusExpanderStyles.xaml" />
</Window.Resources>
<Expander Header="Plus Minus Expander" Style="{StaticResource PlusMinusExpander}" HorizontalAlignment="Right" Width="292">
<Grid Background="Transparent">
<TextBlock>Item1</TextBlock>
</Grid>
</Expander>
即使在添加 CurrencyCellStyle 样式之后,我也希望能够执行 Style="{StaticResource PlusMinusExpander}"
。
我在网上看到过类似的问题,但他们的解决方案中有 none 对我有用。有没有办法同时使用 Style 和 ResourceDictionary?
Window.Resources
属性的类型是ResourceDictionary
,所以不能把两个不同类型的XAML元素当兄弟。相反,您应该:
- 把一个分开的
ResourceDictionary
写到Window.Resources
属性里面,把Style
写在ResourceDictionary
里面。
- 从
ResourceDictionary
. 中删除 x:Key
属性
<FrameworkElement.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/PlusMinusExpanderStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="CurrencyCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="#dddddd" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</FrameworkElement.Resources>
我正在尝试在我的 WPF 程序中使用 ResourceDictionary 和 Style。当我在 <Window.Resources>
中只有 ResourceDictionary 时,一切正常,但只要我添加 <Style>
,程序就会为字典显示 "Resource not found",我得到一个错误 "The resource "PlusMinusExpander" could没有得到解决。"
<Window.Resources>
<Style x:Key="CurrencyCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="#dddddd" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
<ResourceDictionary x:Key="hello" Source="Assets/PlusMinusExpanderStyles.xaml" />
</Window.Resources>
<Expander Header="Plus Minus Expander" Style="{StaticResource PlusMinusExpander}" HorizontalAlignment="Right" Width="292">
<Grid Background="Transparent">
<TextBlock>Item1</TextBlock>
</Grid>
</Expander>
即使在添加 CurrencyCellStyle 样式之后,我也希望能够执行 Style="{StaticResource PlusMinusExpander}"
。
我在网上看到过类似的问题,但他们的解决方案中有 none 对我有用。有没有办法同时使用 Style 和 ResourceDictionary?
Window.Resources
属性的类型是ResourceDictionary
,所以不能把两个不同类型的XAML元素当兄弟。相反,您应该:
- 把一个分开的
ResourceDictionary
写到Window.Resources
属性里面,把Style
写在ResourceDictionary
里面。 - 从
ResourceDictionary
. 中删除
x:Key
属性
<FrameworkElement.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/PlusMinusExpanderStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="CurrencyCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="#dddddd" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</FrameworkElement.Resources>