我有一个带有四列和四行的网格控件的窗口。它包含以下内容:
<Label Name="HeightLabel" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Content="Height" Target="{Binding ElementName=HeightValue}"/>
<TextBox Name="HeightValue" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"/>
<Label Name="WeightLabel" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="2" Content="Weight" Target="{Binding ElementName=WeightValue}"/>
<TextBox Name="WeightValue" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"/>
<Label Name="WidthLabel" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Content="Width" Target="{Binding ElementName=WidthValue}"/>
<TextBox Name="WidthValue" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"/>
<Label Name="LengthLabel" Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="2" Content="Length" Target="{Binding ElementName=LengthValue}"/>
<TextBox Name="LengthValue" Grid.Column="2" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Top" TextAlignment="Center"/>
在资源字典中应用以下样式:
<Style TargetType="{x:Type Label}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border>
<ContentPresenter TextBlock.FontSize="18"
TextBlock.Foreground="Red"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
问题是只有最后一个标签控件被设置样式;其余的都接收默认的TextBlock样式。我知道Label包含一个嵌入式TextBlock,这使得样式比大多数其他控件更难,所以我怀疑存在某种层次结构问题,但对于我的生活,我无法弄明白。
答案 0 :(得分:2)
使用VALID Style时,它们都会在我的屏幕上显示样式。你有几个问题,比如Foreground =“{Red}”而不是Foreground =“Red”,而且缺少一个/&gt;
<Style TargetType="{x:Type Label}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border>
<ContentPresenter TextBlock.FontSize="18"
TextBlock.Foreground="Red"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 1 :(得分:0)
根据我上面的评论,问题是当未指定访问密钥加速器时,Label将呈现为TextBlock,因此我的Label样式未被应用。