自定义按钮的前景色(ControlPresenter)
我正在尝试在 App.xaml 中定义全局按钮样式,并且它基本上按我的预期工作。但是,我只是不知道如何让前台正常工作。无论我做什么,我都会获得默认 TextBlock 的样式(它将颜色设置为白色)。
<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 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>
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.
Thanks,
wTs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像 Markus Hütter 所说,问题可能是您定义了
TextBlock
的隐式样式,并且当Button Content
设置为字符串时,TextBlock< /code> 将在模板中具有
ContentPresenter
的位置创建。这个TextBlock
将拾取隐式样式,这就是您遇到此问题的原因。您可以通过为
string
指定DataTemplate
来禁用为字符串创建的TextBlock
的隐式样式。将以下内容添加到 App.xamlLike Markus Hütter said, the problem is probably that you have an implicit Style for a
TextBlock
defined and when theButton Content
is set to a string, aTextBlock
will be created where you have theContentPresenter
in the Template. ThisTextBlock
will pick up the implicit Style and that's why you're getting this problem.You can disable the implicit Style for a
TextBlock
s that is created in place for a string by specifying aDataTemplate
forstring
. Add the following to App.xaml您似乎正在使用文本块的自定义样式(如果您说前景色是白色),我们可以称之为StandardTextBlockStyle。
在外部网格内的按钮样式内。包括这个:
这应该覆盖默认的 TextBlock 样式。
you seem to be using a custom style for textblock (if you say Foreground color is white) lets call this StandardTextBlockStyle.
Within your button style inside the outer grid. include this:
this should override the default TextBlock style.
我遇到了类似的问题,有一个全局 TextBlock 样式,我无法在需要自定义颜色/字体的其他控件中覆盖它。
基于 Fredrik Hedblad 和Pavlo Glazkov(来自此处)回复,以下解决方案对我有用:
谢谢,
RDV
I had similar issue, there was a global TextBlock style and I was not able to override it in other controls where custom color/font was needed.
Based on Fredrik Hedblad & Pavlo Glazkov (from here) responses, the following solution worked for me:
Thanks,
RDV