Custom Button Foreground Color (ControlPresenter)

I am trying to define a global button style in App.xaml and it basically works as I expect. However, I just can't figure out how to make Foreground work correctly. No matter what I do, I get the default text block style (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 suppose I could change ContentPresenter to TextBlock, which would be good for this particular application, but I'm looking for a more general solution.

Thanks,
Wts

+5
source share
3 answers

Like Markus HΓΌtter, the problem is that you have an implicit style for the TextBlock , and when the Button Content set to a string, a TextBlock will be created where you have the ContentPresenter in the Template. This TextBlock will display implicit style and why you get this problem.

You can disable the implicit style for a TextBlock that is created in place for a string by specifying a DataTemplate for a string . Add the following to your App.xaml application:

 <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> 
+8
source

it looks like you are using a custom style for textblock (if you say the foreground color is white) allows you to call this StandardTextBlockStyle.

Inside the style of your button inside the outer grid. enable 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.

+1
source

I had a similar problem, there was a global TextBlock style, and I could not redefine it in other controls where my own color / font was required.

Based on the answers of Fredrik Hedblad and Pavel Glazkov ( from here ), the following solution worked for me:

 <DataTemplate DataType="{x:Type System:String}"> <TextBlock Text="{Binding}"> <TextBlock.Resources> <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}"> <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground), FallbackValue={StaticResource Colors_ThemeForeground_SCBrush}, TargetNullValue={StaticResource Colors_ThemeForeground_SCBrush}}"/> </Style> </TextBlock.Resources> </TextBlock> </DataTemplate> 

Thank you, RDV

0
source

Source: https://habr.com/ru/post/1343971/


All Articles