重写 WPF 中的主题

发布于 2024-09-16 02:15:20 字数 1224 浏览 4 评论 0原文

我目前正在处理的 wpf 项目有一个小问题。我是 WPF 新手。在我的 app.xaml 中,我为我的应用程序使用 Microsoft Aero 主题。我的 app.xaml 中有类似的内容

 <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.NormalColor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

现在我想知道如何覆盖某些样式属性,例如对于按钮,我想覆盖字体样式,同时保留其余的航空样式。

如果我在窗口资源中定义按钮的样式(例如)

 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
            <Setter Property="Foreground" Value="Red" />
        </Style>

并基于上述样式定义按钮,

则按钮将失去所有航空样式属性。我认为问题在于我为样式定义 BasedOn 属性的方式有关,

BasedOn="{StaticResource {x:Type Button}}"

我不认为资源是静态的,因为它来自 dll,它可能应该是这样的。但不确定。

BasedOn="{DynamicResource {x:Type Button}}"

但上面抛出异常,如果我的 app.xaml 中有多个资源字典(例如 luna 和 classic)怎么办?我如何知道将哪一个用作默认值,同时使用和覆盖另一个(例如 luna)专门用于我的用户界面中的某些控件?那么我的一些按钮将基于 luna 风格,一些按钮将基于 aero 风格并进行一些进一步的修改?

有什么想法吗?

问候,

I have a got a small issue with the wpf project I am currently working on. I am new to WPF. In my app.xaml I am using the Microsoft Aero theme for my application. I have got something like this in my app.xaml

 <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.NormalColor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

Now what I would like to know how to override certain style properties e.g. For Buttons I want to override the font style meanwhile retaining the rest of the aero style.

If I define a style for button in my window resources e.g.

 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
            <Setter Property="Foreground" Value="Red" />
        </Style>

and define a button based on the above style

the button looses all the aero style properties. I think the problem is to do with the way I define the BasedOn property for the style

BasedOn="{StaticResource {x:Type Button}}"

I don't think the resource is static as it comes from a dll, it should probably be something like this. Not sure though.

BasedOn="{DynamicResource {x:Type Button}}"

but the above throws exception and what if I had multiple resourcedictionary in my app.xaml e.g. luna and classic. How can I tell which one to use as the default and at the sametime using and overriding another (e.g. luna) specifically for certain controls in my ui? So some of my buttons will be based on luna style and some on aero style with some further modifications?

Any idea?

Regards,

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

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

发布评论

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

评论(1

奢欲 2024-09-23 02:15:20

对于您的具体问题

例如,对于按钮,我想覆盖字体样式,同时保留其余的航空样式。

我会尝试将其添加到您的资源词典中:

<FontFamily x:Key="YourFont">Tahoma</FontFamily>

然后在您的样式中使用它:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
     <Setter Property="FontFamily" Value="{StaticResource YourFont}"/>
</Style>

这样您就可以根据一个键更改 FontFamily。

我建议查看此 http://wpfthemes.codeplex.com/ SourceControl/latest#WPF.Themes.Demo/Window1.xaml

For your specific question on

e.g. For Buttons I want to override the font style meanwhile retaining the rest of the aero style.

I'd try adding this in your Resource Dictionary:

<FontFamily x:Key="YourFont">Tahoma</FontFamily>

Then use this in your Style:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
     <Setter Property="FontFamily" Value="{StaticResource YourFont}"/>
</Style>

So you could change the FontFamily based on one single key.

I recommend looking at this http://wpfthemes.codeplex.com/SourceControl/latest#WPF.Themes.Demo/Window1.xaml

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