如何覆盖特定主题中的样式

发布于 2024-10-11 15:45:19 字数 970 浏览 3 评论 0原文

我试图覆盖主题文件中基本控件(TextBox、ComboBox)的默认样式。 像这样:

在主题/classic.xaml中

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Black"/>
</Style>

在主题/Aero.NormalColor.xaml中

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Green"/>
</Style>

但这似乎不起作用。我总是得到默认的样式,没有任何改变。我什至用特定的键尝试过 就像

<Style x:Key="DefaultTextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Green"/>
</Style>

在 xaml 中声明控件时, 并始终使用此键。似乎什么都不起作用。

如果我将样式放入 application.xaml 文件中,我没有问题,但我希望此样式与主题相关。 顺便说一句,它与我自己的用户控件配合得很好。

有人可以告诉我我在这里做错了什么吗?

我知道一个解决方案可能是使用一个类来查看我使用的主题并使用某种触发器,但我真的想在 xaml 中执行此操作。

谢谢

I'm trying to override the default style of a base control (TextBox, ComboBox) in a theme-file.
Like this:

in themes/classic.xaml

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Black"/>
</Style>

in themes/Aero.NormalColor.xaml

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Green"/>
</Style>

But this doesn't seem to work. I always get the defaulted style with no changes. I even tried it with a specific key
like

<Style x:Key="DefaultTextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Green"/>
</Style>

and always use this key when declaring the control in xaml. Nothing seems to work.

If I put the style in the application.xaml file I have no problem but I want this style to be theme dependent.
By the way, it works well with my own usercontrols.

Can someone tell me what I'm doing wrong here?

I know one solution could be to use a class to see wich theme I use and to use a trigger of somesort, but I really wanted to do this in xaml.

Thanks

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

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

发布评论

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

评论(3

戒ㄋ 2024-10-18 15:45:19

您是否在某处引用了您的主题?
例如

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Test.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Simple Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

,另外,不要在主题中使用 x:Key,除非它只是主题内由另一个样式引用的子样式。

Did you reference your theme somewhere?
e.g.

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Test.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Simple Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Also, don't use the x:Key in themes unless it's just a sub-style that is referenced inside the theme by another style.

你另情深 2024-10-18 15:45:19

不幸的是,ThemeInfo 机制仅用于同一程序集中定义的控件。您想要的是为不属于您自己的控件加载特定于主题的资源。我还没有测试过,但我认为您需要的是 主题字典标记扩展ThemeDictionary 从程序集中加载适合主题的ResourceDictionary

这里有人谁去静态地工作,这似乎就是你想要的。

Unfortunately, the ThemeInfo mechanism is only used for controls defined in the same assembly. What you want is to load theme-specific resources for controls that are not your own. I haven't tested it but I think what you need is the ThemeDictionary Markup Extension. A ThemeDictionary loads a theme-appropriate ResourceDictionary from an assembly.

Here is someone who go it to work statically, which appears to be what you want.

薄荷→糖丶微凉 2024-10-18 15:45:19

问题是你得到了循环引用。这是因为您定义了类 XXX 的样式,但也使用与基础资源相同的资源。

我“很久”前写了一篇关于此的博客文章,它解释了该怎么做:

http://blog.catenalogic.com/post/2009/07/20/Override-or-customize-WPF-themes-such-as- PresentationFrameworkAero.aspx

技巧是使用键“DefaultTextBoxStyle”在同一资源字典中定义样式,然后使用右键(“{x:Type TextBox}”)以编程方式将样式添加到主应用程序资源这样,您就可以避免循环引用。

The problem is that you are getting circular references. This is because you define a style with class XXX, but also use the same resource as base resource.

I have wrote a blog article about this a "long" time ago, it explains what to do:

http://blog.catenalogic.com/post/2009/07/20/Override-or-customize-WPF-themes-such-as-PresentationFrameworkAero.aspx

The trick is to define a style in the same resource dictionary with the key "DefaultTextBoxStyle", and then programatically add the styles with the right key ("{x:Type TextBox}" the to main application resource. This way, you avoid the circular references.

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