【问题标题】:How to base a style on another style in a resource dictionary?如何基于资源字典中的另一种样式创建样式?
【发布时间】:2011-08-06 05:02:08
【问题描述】:

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

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

【问题讨论】:

    标签: wpf xaml styles


    【解决方案1】:

    为您的基本样式命名,例如 FooStyle。

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

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

    【讨论】:

    • 我可以不指定名称吗?字典中的控件只有一种样式。
    • 您的意思是,您希望扩展样式根据其 TargetType 确定要扩展的样式,而不是为基本样式分配键?我不知道。
    • 你为什么犹豫给基本样式一个 x:Key="FooStyle" ?
    • @PieterMüller:如果您为样式分配键,则默认情况下,上下文中的所有匹配控件都不再使用该样式。 Nathan Friend 的回答为此提供了解决方案。我刚遇到同样的问题:)
    【解决方案2】:

    我认为没有为“控制”定义基本样式,所以您的 BasedOn="{StaticResource {x:Type Control}}" 部分找不到任何东西。

    你可能想要改变

    <Style x:Key="ValidTrigger" TargetType="Control" BasedOn="{StaticResource {x:Type Control}}" >
    

    <Style x:Key="ValidTrigger" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" >
    

    【讨论】:

      【解决方案3】:

      我过去使用过的一个技巧:在为您的应用程序定义整体样式的 ResourceDictionary 中,将 x:Key 添加到您想要继承的样式中,如下所示:

      <Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
        <!-- your style here -->
      </Style>
      

      要将此样式应用于指定类型的所有控件(在此示例中为 Button),而不必显式设置每个 ButtonStyle 属性,请添加另一个样式 BasedOn 此样式,但不'没有钥匙:

      <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle}" />
      

      使用此方法,您的应用程序中的所有Buttons 将自动继承您的自定义样式,但您仍然可以在ButtonStyle 条目中创建其他样式BasedOn,并避免破坏您的所有自定义样式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 1970-01-01
        相关资源
        最近更新 更多