user3821206
user3821206

Reputation: 619

change the text color when hover on a Button in a UWP

I am trying to modify the Text color of a Button when Mouse hover,from the black to another color,I used this Style:

  <Style  x:Key="ButtonmenuStyle"
    TargetType="Button">
        <Setter Property="Foreground" Value="#393185"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid>
                            <ContentPresenter x:Name="Content1"/>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

but the problem with this code, is that I get a result in hover(the color on hover has changed),but the Text of the Button is not displayed in the Left center,this is the result:

enter image description here

and this is my code,to apply the style on the button:

<Button  BorderThickness="0" Background="Transparent" Content="Nouveau Local" FontSize="18"  Foreground="#393185"  x:Name="res9"  HorizontalAlignment="Left"   Height="50" Grid.Column="1" Style="{Binding Source={StaticResource ButtonmenuStyle}}"/>

please how can I correct my code,to have a text centred in Left thanks for help

Upvotes: 1

Views: 2868

Answers (1)

Kory Gill
Kory Gill

Reputation: 7163

If you are asking to vertically center the content of your button, you can do this:

<ContentPresenter x:Name="Content1" VerticalAlignment="Center"/>

Upvotes: 1

Related Questions