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
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.
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>
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:
<Grid x:Name="gridMainButton">
<Grid.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource StandardTextBlockStyle}">
<Setter Property="Foreground" Value="Black"/>
</Style>
</Grid.Resources>
<!-- ...-->
</Grid>
this should override the default TextBlock style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With