gpt4 book ai didi

WPF 替换和扩展样式

转载 作者:行者123 更新时间:2023-12-02 00:30:55 31 4
gpt4 key购买 nike

如何更改 WPF 对控件的默认 样式的看法?为什么首先会发生这种情况?在下面的 XAML 中,我为 Button 声明了一个 Style,然后进一步声明了一个新的 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 继承了我未在未命名样式中从按钮的系统默认主题(在我的例子中为 Aero)中指定的所有值。

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

最佳答案

看来答案就在这里:

http://wpfthemereplacer.codeplex.com/

来自网站描述:

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.

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

关于WPF 替换和扩展样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6839383/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com