>我正在尝试为所有标签添加全局隐式样式。我定义了以下样式:
<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>
但它似乎并没有改变任何东西。我也尝试将其包含在我的一个主题文件中,但它也不起作用。我尝试在一个新的项目中执行此操作,以查看当前项目中是否存在与不同样式的冲突,但事实似乎并非如此。
您正在使用没有背景属性的视图框。您正在覆盖模板,然后我们看不到标签的背景。我将把视图框放在边框内,在那里我会放颜色。
<Border Background="Green">
<Viewbox.../>
</Border>
让我知道解决方案是否有效。