WPF 替换和扩展样式

发布于 2024-11-26 15:47:16 字数 1307 浏览 1 评论 0原文

如何更改 WPF 对控件的默认样式的理解?为什么会发生这种情况?在下面的 XAML 中,我声明了 Button 的样式,然后进一步声明了一个新的样式,该样式将覆盖名为“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)中指定的所有值。

我可以做什么来改变这种行为?

How do I change what WPF's idea of the default style for a control is? And why is this happening in the first place? In the below XAML, I declare a Style for Button, and then further declare a new Style that overrides one of the setters called "HugeBut". I would expect that HugeBut is implicitly BasedOn my new un-named style, but apparently it is not;

<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}"/>

You would expect two red buttons, one with black text and one with yellow, but Style HugeBut inherits all of the values that I did not specify in my unnamed style from the system default theme for Button (Aero in my case).

What can I do to change this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放手` 2024-12-03 15:47:16

看来答案就在这里:

http://wpfthemereplacer.codeplex.com/

从网站描述中:

该库允许用户提供自己的资源字典
替换 WPF 加载的默认主题字典。这使得
所以你不必用装饰自定义样式
BasedOn="{StaticResource {x:Type ...}}" 当您自己的自定义主题是
在您的应用程序中使用。如果您有自定义的话,它也可以做到这一点
仅提供增强功能且不需要的控件
替换样式,您不需要定义新样式或
创建自定义控件时覆盖 DefaultStyleKey。

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

It appears that the answer is here:

http://wpfthemereplacer.codeplex.com/

From the site description:

This library allows users to provide their own resource dictionaries
to replace the default theme dictionaries loaded by WPF. This makes it
so you don't have to decorate custom styles with
BasedOn="{StaticResource {x:Type ...}}" when your own custom theme is
being used in your application. It also makes it so if you have custom
controls that just provide enhanced capability and don't need to
replace the the style, you don't need to define a new style or
override the DefaultStyleKey when you create the custom control.

This is exactly what I'm looking for. This will allow me to use Styles as they are meant to be used across an app that has been extensively "re-themed", rather than theme-ing by setting global styles (and then deal with tracking down bits of code that are missing BasedOn, or cannot deal with it at all due to WPF bugs and other constraints)

扛刀软妹 2024-12-03 15:47:16

可以工作,但我不想显式设置基于。

好吧,框架并不真正关心你是否愿意,据我所知,你必须

works, but I do not want to explicitly set the based-on.

Well, the framework does not really care if you don't want to, for all i know, you have to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文