代码之家  ›  专栏  ›  技术社区  ›  Deadzone

未应用全局标签样式

  •  0
  • Deadzone  · 技术社区  · 7 年前

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

    <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 回复  |  直到 7 年前
        1
  •  1
  •   Kaspar    7 年前

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