前景不适用于标签
Foreground does not apply for labels
我有一些这样的标签:
<Label x:Name="ledCalculate" Width="Auto" Content="Calculate" Height="25" FontFamily="Cambria" FontSize="18.667" VerticalContentAlignment="Bottom" Padding="10,0,0,0" BorderThickness="0,0,0,1" Style="{StaticResource RightPanelLabels}" Grid.Row="11"/>
我为它定义了一个样式:
<Style TargetType="Label" x:Key="RightPanelLabels" BasedOn="{StaticResource
{x:Type Label } }">
<Setter Property="Foreground" Value="#FFEABEBE"></Setter>
<Setter Property="BorderBrush" Value="#FF818080"></Setter>
<Setter Property="BorderThickness" Value="0,0,0,1"></Setter>
</Style>
样式中的边框粗细setter适用于控制,在样式中具有较高的优先级,visual studio在局部忽略其值,但样式中的前景setter不适用,当我在本地设置它时它适用,
为什么样式中的边框厚度优先,而局部中的前景优先???????
我知道像波纹管这样的标签有一个默认模板,它有 IsEnabled
的触发器,该模板是这样的:
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
谁能帮帮我?
对不起我的英语。
您必须更改 属性 样式中的名称 setter
<Setter Property="TextBlock.Foreground" Value="#FFEABEBE">
所有本地样式都优先于定义的样式,因此内联样式将始终覆盖资源中定义的其他样式。
当我 运行 代码时,我发现 Style 的 BorderThickness
也被 BorderThickness
的本地内联样式值覆盖。
我也很想知道是否有解决方法。
我也找到了这篇文章,希望对您有所帮助
编辑:
尝试了几种不同的方法后,我发现使用这种方法我们可以覆盖标签的内联 Foreground
样式,但我不知道这是否是最健康的方法
<Style TargetType="{x:Type Label}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}"
Foreground="Red" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Label FontSize="50" Foreground="Black"> I am Black inline, Red on Style </Label>
这是我刚刚编辑 Label
的 ContentTemplate
。
基本上,这对于 Label
是可能的,因为 Label
派生自 ContentControl
而 TextBlock
位于 System.Windows.Controls
命名空间中,它不是控件。它直接来自 FrameworkElement
。
TextBlock
在许多其他控件中用于显示文本,包括在 Label
.
中
我有一些这样的标签:
<Label x:Name="ledCalculate" Width="Auto" Content="Calculate" Height="25" FontFamily="Cambria" FontSize="18.667" VerticalContentAlignment="Bottom" Padding="10,0,0,0" BorderThickness="0,0,0,1" Style="{StaticResource RightPanelLabels}" Grid.Row="11"/>
我为它定义了一个样式:
<Style TargetType="Label" x:Key="RightPanelLabels" BasedOn="{StaticResource
{x:Type Label } }">
<Setter Property="Foreground" Value="#FFEABEBE"></Setter>
<Setter Property="BorderBrush" Value="#FF818080"></Setter>
<Setter Property="BorderThickness" Value="0,0,0,1"></Setter>
</Style>
样式中的边框粗细setter适用于控制,在样式中具有较高的优先级,visual studio在局部忽略其值,但样式中的前景setter不适用,当我在本地设置它时它适用,
为什么样式中的边框厚度优先,而局部中的前景优先???????
我知道像波纹管这样的标签有一个默认模板,它有 IsEnabled
的触发器,该模板是这样的:
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
谁能帮帮我? 对不起我的英语。
您必须更改 属性 样式中的名称 setter
<Setter Property="TextBlock.Foreground" Value="#FFEABEBE">
所有本地样式都优先于定义的样式,因此内联样式将始终覆盖资源中定义的其他样式。
当我 运行 代码时,我发现 Style 的 BorderThickness
也被 BorderThickness
的本地内联样式值覆盖。
我也很想知道是否有解决方法。
我也找到了这篇文章,希望对您有所帮助
编辑:
尝试了几种不同的方法后,我发现使用这种方法我们可以覆盖标签的内联 Foreground
样式,但我不知道这是否是最健康的方法
<Style TargetType="{x:Type Label}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}"
Foreground="Red" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Label FontSize="50" Foreground="Black"> I am Black inline, Red on Style </Label>
这是我刚刚编辑 Label
的 ContentTemplate
。
基本上,这对于 Label
是可能的,因为 Label
派生自 ContentControl
而 TextBlock
位于 System.Windows.Controls
命名空间中,它不是控件。它直接来自 FrameworkElement
。
TextBlock
在许多其他控件中用于显示文本,包括在 Label
.