默认样式不适用于子类 [英] Default styles do not apply to subclasses

查看:59
本文介绍了默认样式不适用于子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的场景,涉及在WPF中覆盖默认样式并将其应用于子类.这不可能吗?例如,我有以下代码:

I have a weird scenario involving overriding default styles in WPF and having them apply to subclasses. Is this not possible? For example, I have the following code:

<Window x:Class="TestWPFStyling.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:testWpfStyling="clr-namespace:TestWPFStyling"
        Title="MainWindow" Height="350" Width="525" Background="White">
    <Window.Resources>
        <Style TargetType="testWpfStyling:SomeLabel">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <testWpfStyling:SomeLabel Grid.Row="0">This should be red.</testWpfStyling:SomeLabel>
        <testWpfStyling:YellowLabel Grid.Row="1">This should be red.</testWpfStyling:YellowLabel>
        <StackPanel Grid.Row="2">
            <StackPanel.Resources>
                <Style TargetType="testWpfStyling:SomeLabel">
                    <Setter Property="Foreground" Value="Blue" />
                </Style>
            </StackPanel.Resources>
            <testWpfStyling:SomeLabel>This should be blue.</testWpfStyling:SomeLabel>
            <testWpfStyling:YellowLabel>This should be blue.</testWpfStyling:YellowLabel>
        </StackPanel>

    </Grid>
</Window>

具有以下代码:

namespace TestWPFStyling
{
    public partial class MainWindow : Window
    {
    }

    public class SomeLabel : Label
    {
    }

    public class YellowLabel : SomeLabel
    {
    }
}

我希望StackPanel中的控件YellowLabel的颜色为蓝色,外面的控件为红色,但是它们都是黑色的.子类是否可以采用其父代的默认样式?

I would expect the control YellowLabel in the StackPanel to have a color of Blue and the one outside to be red, however both of them are black. Is there a way for a subclass to take on the default style of its parent?

推荐答案

实际上,这就是WPF的工作方式.我有一篇博客文章,其中讨论了WPF样式(主要是关于我们的Theme属性的工作方式),但是问题1的情况1的根本问题与您所描述的相同.也就是说,隐式本地样式是使用实际的类类型定位的,并且与DefaultStyleKey没有关系.仅在查找默认样式(即基于当前OS主题和控件的默认generic.xaml应用于控件的样式)时才使用DefaultStyleKey.解决此问题的一种简单方法是将派生控件的样式(例如,在其ctor中)设置为对基类样式的DynamicResource引用.例如

Actually this is how WPF works. I have a blog post that discusses WPF styling (primarily with regards to how our Theme property works) but the underlying issue for scenario 1 of problem 1 is the same as what you are describing. That is, that implicit local styles are located using the actual class type and have no relation to the DefaultStyleKey. The DefaultStyleKey is only used when locating the default style (i.e. the style that is applied to the control based on the current OS theme and the default generic.xaml for the control). One easy way to address this would be to set the Style of your derived control (e.g in its ctor) to a DynamicResource reference to the base class' style. E.g.

this.SetResourceReference(StyleProperty, typeof(SomeLabel));

这篇关于默认样式不适用于子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆