【发布时间】:2016-10-28 21:40:46
【问题描述】:
Button 的我的全局 (App.xaml) ControlTemplate 不想从默认的黑色覆盖 Foreground 属性。这似乎是一个相对常见的问题,但我尝试了各种解决方案,包括将BasedOn 设置为null 为described in this question。
我的代码如下,你可以看到我已经尝试在Grid 和ContentPresenter 上明确声明前景色。然而颜色保持黑色。 Snoop 告诉我它是默认继承的,但似乎没有说明从哪里继承,并将父 ContentPresenter 显示为将 TextElement.Foreground 设置为正确的颜色。
我应该在这段代码中设置什么吗?我错过了一个元素或属性吗?
<Style TargetType="Button">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" TextBlock.Foreground="#FFD3D3D2">
<Border x:Name="border" Background="Transparent" BorderBrush="#FF5C7999" BorderThickness="2" CornerRadius="{Binding RelativeSource={RelativeSource TemplatedParent},Path=ActualHeight,Converter={StaticResource HalfConverter}}" Padding="10">
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0" TextBlock.Foreground="#FFD3D3D2">
</ContentPresenter>
</Border>
</Grid>
...
【问题讨论】:
-
我无法根据您那里的内容重现该问题。按钮出现在什么上下文中?你用什么
Content? -
实际上,我可以重现它:
<Button Content="Foo" />给我一个灰色的“Foo”。<Button><Label>Foo</Label></Button>给我一个黑色的“Foo”。 -
我可以在
ControlTemplate.Resources中为Label添加一个隐含的Style,这将应用于Label内容。 -
@EdPlunkett 我只看到了您的第一个答案并开始检查内容。请参阅我发布的答案 - 做到了。但是,如果您愿意,我很乐意接受您解释上述内容的答案。
-
是的,这就是我提出这个问题的地方。我们可能同时对其进行了测试。
标签: c# wpf xaml wpf-controls