如何使资源字典中的一种样式基于另一种样式?
我有一个应用于资源字典中所有按钮的主题。现在我想在从字典继承样式更改的同时向按钮添加触发器。我尝试了以下代码,但它说找不到该控件。我该如何解决它?
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<conv:ErrorContentConverter x:Key="ErrorContentConverter" />
<Style x:Key="ValidTrigger"
TargetType="Control" BasedOn="{StaticResource {x:Type Control}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsValid}" Value="False">
<Setter Property="IsEnabled" Value="false" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</UserControl.Resources>
基本模板:
<Style TargetType="{x:Type Button}" BasedOn="{x:Null}">
<Setter Property="FocusVisualStyle"
Value="{DynamicResource NuclearButtonFocusVisual}" />
<Setter Property="Foreground" Value="#FF042271" />
<Setter Property="FontFamily" Value="Trebuchet MS" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Padding" Value="3" />
<Setter Property="Template" Value="{DynamicResource ButtonTemplate}" />
</Style>
I have a theme that is applied to all buttons in a resource dictionary. Now I want to add a trigger to the button while inheriting the style changes from the dictionary. I tried the following code, but it says that the control cannot be found. How can I fix it ?
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<conv:ErrorContentConverter x:Key="ErrorContentConverter" />
<Style x:Key="ValidTrigger"
TargetType="Control" BasedOn="{StaticResource {x:Type Control}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsValid}" Value="False">
<Setter Property="IsEnabled" Value="false" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</UserControl.Resources>
The base template:
<Style TargetType="{x:Type Button}" BasedOn="{x:Null}">
<Setter Property="FocusVisualStyle"
Value="{DynamicResource NuclearButtonFocusVisual}" />
<Setter Property="Foreground" Value="#FF042271" />
<Setter Property="FontFamily" Value="Trebuchet MS" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Padding" Value="3" />
<Setter Property="Template" Value="{DynamicResource ButtonTemplate}" />
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我过去使用过的一个技巧:在为应用程序定义总括样式的
ResourceDictionary
中,将x:Key
添加到您想要继承的样式,如下所示:将此样式应用于指定类型的所有控件(本例中为
Button
),而无需显式设置每个Button< 的
Style
属性/code>,添加另一个BasedOn
样式,但没有键:使用此方法,应用程序中的所有
Button
将自动继承您的自定义样式,但您仍然可以BasedOn
ButtonStyle
条目创建其他样式,并避免破坏所有自定义样式。One trick I've used in the past: in your
ResourceDictionary
that defines blanket styles for your application, add anx:Key
to the style you'd like to inherit from, like so:To apply this style to all controls of the specified type (
Button
in this example) without having to explicitly set theStyle
attribute of everyButton
, add another style that'sBasedOn
this style, but doesn't have a key:Using this method, all
Button
s in your application will automatically inherit your custom style, but you can still create additional stylesBasedOn
theButtonStyle
entry and avoid blowing away all of your custom styling.为您的基本样式命名,例如 FooStyle。
在您提供的示例中,将 TargetType 和 BasedOn 修改为如下所示:
Give your base Style a name, say FooStyle.
In the example you gave, modify the TargetType and BasedOn to look as follows:
我认为没有为“控制”定义基本样式,所以你的
BasedOn="{StaticResource {x:Type Control}}"
部分找不到任何内容。您可能想更改
为
I think there is no base style defined for "control" so your
BasedOn="{StaticResource {x:Type Control}}"
part won't find anything.You probably want to change
to