自定义按钮(ControlPresenter)的前景色 [英] Foreground Color of Custom Button (ControlPresenter)
问题描述
我正在尝试在App.xaml中定义全局按钮样式,并且大多数情况下都可以按照我的期望进行工作.但是,我只是无法弄清楚如何使前景正常工作.无论我做什么,我都会得到默认TextBlock的样式(它将颜色设置为白色).
I am attempting to define a global button style in App.xaml, and it's mostly working as I expect. However, I just cannot figure out how to get the Foreground to work correctly. No matter what I do, I am getting the style of the default TextBlock (which sets the color to White).
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="3, 5" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="FocusVisualStyle"
Value="{StaticResource ButtonFocusVisual}" />
<Setter Property="Foreground" Value="Red" />
<Setter Property="Padding" Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="gridMainButton"
RenderTransformOrigin="0.5, 0.5">
<Grid.RenderTransform>
<ScaleTransform x:Name="scaleTransform"
CenterX="0.5"
CenterY="0.5" />
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates" >
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="scaleTransform"
Storyboard.TargetProperty="ScaleX"
Duration="0"
To="0.85" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="ellipse"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
StrokeThickness="2"
Stroke="{StaticResource standardBackground}"
Fill="{StaticResource standardBackground}" />
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="4, 8"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想我可以将ContentPresenter更改为TextBlock,这对于该特定应用程序是可以的,但是我正在寻找更通用的解决方案.
I guess I could change the ContentPresenter to a TextBlock, which would be ok for this particular application, but I'm looking for a more generic solution.
谢谢,
wTs
Thanks,
wTs
推荐答案
就像MarkusHütter所说的那样,问题可能在于您为TextBlock
定义了隐式样式,并且当Button Content
设置为字符串时,在模板中有ContentPresenter
的位置将创建一个TextBlock
.此TextBlock
将获取隐式样式,这就是为什么您遇到此问题的原因.
Like Markus Hütter said, the problem is probably that you have an implicit Style for a TextBlock
defined and when the Button Content
is set to a string, a TextBlock
will be created where you have the ContentPresenter
in the Template. This TextBlock
will pick up the implicit Style and that's why you're getting this problem.
您可以通过为string
指定一个DataTemplate
来禁用为字符串创建的TextBlock
的隐式样式.将以下内容添加到App.xaml
You can disable the implicit Style for a TextBlock
s that is created in place for a string by specifying a DataTemplate
for string
. Add the following to App.xaml
<Application ...>
<Application.Resources>
<DataTemplate xmlns:sys="clr-namespace:System;assembly=mscorlib"
DataType="{x:Type sys:String}">
<TextBlock Text="{Binding}">
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}"/>
</TextBlock.Resources>
</TextBlock>
</DataTemplate>
<!--...-->
</Application.Resources>
</Application>
这篇关于自定义按钮(ControlPresenter)的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!