I'm trying to change the font color of a button when the button is clicked. I have tried many different things but none have worked. This is the latest thing I tried and what I believe should be the answer but its not working. Can someone help? PointerOver isworking fine, but Focused is not doing anything when the button is clicked.
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid
x:Name="RootGrid"
Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="ContentPresenter.FontWeight" Value="Bold" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter
x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTransitions="{TemplateBinding ContentTransitions}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
EDIT based on comments: Adding clarification- I tried using Pressed prior to posting this question but it doesn't do what I'm looking for. After click I want the color to remain applied until another button is clicked, then the text in the new button clicked will change color and the previous button clicked will go back to the default color. With pressed, as soon as you let the mouse button go the text goes back to default.
Edit based Answer suggestions: Also providing surrounding code to see if mistake is being made somewhere else.
<Page.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="UseSystemFocusVisuals" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="ContentPresenter.FontWeight" Value="Bold" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled">
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTransitions="{TemplateBinding ContentTransitions}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Style="{StaticResource ButtonStyle}" Content="Button" HorizontalAlignment="Left" Margin="10,180,0,0" VerticalAlignment="Top" />
<Button Style="{StaticResource ButtonStyle}" Content="Button" HorizontalAlignment="Left" Margin="100,180,0,0" VerticalAlignment="Top"/>
<Button Style="{StaticResource ButtonStyle}" Content="Button" HorizontalAlignment="Left" Margin="194,180,0,0" VerticalAlignment="Top"/>
</Grid>
In UWP, there's a System-level focus visual applied to
Button
and a lot of other controls by default. If you want to manage your own focus visual like how you defined in yourFocused
visual state, try settingin your style.
Also, custom focus visual state is not inside
CommonStates
butFocusStates
. So you will need to move your focus state insideUpdate
When you want to maintain the focus on click, you will want two things. First, you need
AllowFocusOnInteraction
set toTrue
. By default, it's alreadyTrue
on aButton
. Second, you want to handlePointerFocused
state along withFocused
one. So replace your focus states with the followingWhy? Because two states are happening around the same time when you click a
Button
-Pressed
fromCommonStates
andPointerFocused
fromFocusStates
. TheFocused
state is generally triggered when you tap the Tab key.