嗨,这一切都是一个让我疯狂了几天的问题。
简单地说,每当我在从TextBlock控件派生的任何东西上声明前景颜色时,都会在设计时识别前景颜色,但在运行时总是默认为黑色。
这就好像在控件上忽略了前台属性一样。
因此,例如,通常我希望下面的按钮显示一个白色文本:
<Button x:Name="MyButton" Content="Hello World" Foreground="White" ... />但是,这会呈现一个按钮,前景文本颜色是黑色的。它实际上忽略了前台设置器属性。
要使它像预期的那样工作,唯一的方法是做以下工作:
<Button x:Name="MyButton" .... >
<TextBlock Text="Hello World" Foreground="White"/>
</Button>这种方式工作,按钮正确地呈现白色文本。但是我知道我不应该像这样显式地定义按钮文本块。
从文本块派生的任何内容都会发生相同的行为。
有人知道为什么会这样吗?
更新:我已经检查了应用于TextBox的样式的解决方案。我在TextBlock上定义了自己的风格,即:
<Style x:Key="TextBlockText" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="#FF63798F"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>您可以看到,它为前台定义了一个值。然而,,,当我从资源字典中删除这种样式时,上面的问题仍然存在。
更多的信息是,我正在使用MahApps.Metro库,我想知道这是否是造成问题的原因。
还有人有其他的想法吗?甚至想到哪里去调查??
发布于 2013-08-18 09:47:28
WPF中的每个控件都有一个与其相关联的模板。我想在你的按钮上定义了一种样式,它不包括前台属性。
例如,
<Style x:Key="DialogButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding BorderBrush}"/>
<TextBlock Foreground="{TemplateBinding Foreground}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>可以使用Style={StaticResource DialogButtonStyle}并为按钮定义前台吗?参见这里,我们在TemplateBinding的前台使用了TextBlock,而不是定义其内部的颜色。
发布于 2013-08-18 09:54:42
我建议保持TextBlock样式的原样,并且在Button或其他控件中,在模板级别上添加一个新的具有TextBlock样式的资源。它将位于该模板的域中,因此不会影响其他文本块,但会覆盖主TextBlock的样式。
例如:
<ControlTemplate>
<Grid>
<Grid.Resources>
<!-- Put a new/duplicate TextBlock Style here with
the appropriate Foreground color, or TemplateBinding
and it will override it for this Grid's children -->
</Grid.Resources>
<TextBlock />
</Grid>
</ControlTemplate>https://stackoverflow.com/questions/18297722
复制相似问题