首页 文章

WPF替换和扩展样式

提问于
浏览
3

如何更改WPF对控件的默认样式的想法?为什么这首先发生?在下面的XAML中,我声明了一个Style for Button,然后进一步声明一个新的Style,它覆盖了一个名为"HugeBut"的setter . 我希望HugeBut是隐含的基于我新的未命名的风格,但显然它不是;

<Window.Resources>
  <Style TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
          <Border Background="Red">
            <ContentPresenter/>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!-- badness -->
  <Style TargetType="{x:Type Button}" x:Key="HugeBut">
    <Setter Property="Foreground" Value="Yellow"/>
  </Style>

  <!-- works, but I do not want to explicitly set the based-on. -->
  <Style TargetType="{x:Type Button}" x:Key="HugeBut" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Foreground" Value="Yellow"/>
  </Style>
</Window.Resources>

<Button Content="Regular" />
<Button Content="huge!" Style="{StaticResource HugeBut}"/>

您可能会看到两个红色按钮,一个带有黑色文本,另一个带有黄色,但Style HugeBut继承了我在未命名样式中未指定的所有值,这些值来自Button的系统默认主题(在我的案例中为Aero) .

我该怎么做才能改变这种行为?

2 回答

  • 0

    似乎答案在这里:

    http://wpfthemereplacer.codeplex.com/

    从网站描述:

    此库允许用户提供自己的资源字典来替换WPF加载的默认主题字典 . 这样就可以在您的应用程序中使用自己的自定义主题时,不必使用BasedOn =“{StaticResource {x:Type ...}}”来装饰自定义样式 . 它也是如此,如果你有自定义控件只提供增强功能而不需要替换样式,则在创建自定义控件时不需要定义新样式或覆盖DefaultStyleKey .

    这正是我正在寻找的 . 这将允许我使用样式,因为它们旨在用于广泛“重新主题化”的应用程序,而不是通过设置全局样式进行主题化(然后处理追踪缺少的代码的一些代码)由于WPF错误和其他限制,或根本无法处理它)

  • 3

    工作,但我不想明确设置基于 .

    好吧,如果你不想,我不知道你需要的框架并不在乎 .

相关问题