如何在资源字典中基于另一种样式的样式? [英] How to base a style on another style in a resource dictionary?

查看:25
本文介绍了如何在资源字典中基于另一种样式的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用于资源字典中所有按钮的主题.现在我想在从字典继承样式更改的同时向按钮添加触发器.我尝试了以下代码,但它说找不到控件.我该如何解决?

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>

基础模板:

    <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>

推荐答案

为您的基本 Style 命名,例如 FooStyle.

Give your base Style a name, say FooStyle.

在您提供的示例中,将 TargetType 和 BasedOn 修改为如下所示:

In the example you gave, modify the TargetType and BasedOn to look as follows:

 <Style x:Key="ValidTrigger" 
        TargetType="{x:Type Control}" BasedOn="{StaticResource {x:Type Control}}" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsValid}" Value="False">
            <Setter Property="IsEnabled" Value="false" />
        </DataTrigger>
    </Style.Triggers>
</Style>

这篇关于如何在资源字典中基于另一种样式的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆