Jeya Sri
Jeya Sri

Reputation: 73

Changing Button Foreground using VisualStateManager in uwp

I try to change the button foreground color using visual sate manager its does not work at all.

   <Button x:Name="Close" HorizontalAlignment="Stretch" 
                                            Width="100" Background="#FF4F4F4F" 
                                            Height="50" BorderThickness="2" BorderBrush="#FF2F2F2F"
                                            Content="Cancel">
                                        <VisualStateManager.VisualStateGroups>
                                            <VisualStateGroup x:Name="CommonStates">
                                                <VisualState x:Name="PointerOver">
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Close"
                                                                   Storyboard.TargetProperty="Foreground">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="#FFFFFFFF" />
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                                <VisualState x:Name="Pressed">
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Close"
                                                                   Storyboard.TargetProperty="Foreground">
                                                            <DiscreteObjectKeyFrame KeyTime="0" Value="#FFFFFFFF" />
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </VisualState>
                                            </VisualStateGroup>
                                        </VisualStateManager.VisualStateGroups>
                                    </Button>

please help me to work this.

Thanks in advance.

Upvotes: 2

Views: 739

Answers (1)

Victory Jessie
Victory Jessie

Reputation: 679

It is not working because you were trying to set Color value for a Brush type.

Please create a Brush StaticResource using the color you need. And set the resource in animation.

<SolidColorBrush x:Key="TextBoxErrorThemeBrush" Color="Red" />

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PART_TextBlock">
     <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxErrorThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>

Regards,

Jessie

Upvotes: 3

Related Questions