全局标签样式未应用

时间:2018-08-08 08:45:45

标签: c# wpf xaml

我正在尝试为所有标签添加全局隐式样式。我定义了以下样式:

<Style x:Key="LabelStyle" TargetType="{x:Type Label}">
    <Setter Property="Background" Value="Green"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <Viewbox StretchDirection="DownOnly" HorizontalAlignment="Left">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="True" />
                </Viewbox>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我尝试将其放入App.xaml文件中

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="LabelStyle" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="Green"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Label">
                        <Viewbox StretchDirection="DownOnly" HorizontalAlignment="Left">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              RecognizesAccessKey="True" />
                        </Viewbox>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!--Merge Dictionaries...-->
    </ResourceDictionary>
</Application.Resources>

但是它似乎并没有改变任何东西。我也曾尝试将其包含在我的主题文件之一中,但它也不起作用。我尝试在一个新项目中进行此操作,以查看当前项目中是否存在任何风格不同的冲突,但事实并非如此。

1 个答案:

答案 0 :(得分:1)

您正在使用不具有Background属性的Viewbox。您正在覆盖模板,然后我们看不到标签的背景。我将把viewbox放在边框内,在其中放置颜色。

<Border Background="Green">
    <Viewbox.../>
</Border>

让我知道解决方案是否有效。

相关问题