How to use VisualStateManager correctly?

My code for changing properties does not work, and I absolutely do not know what is wrong, but maybe you are doing it.

Here is a simplified example of my code that reproduces the error. This is my Xaml code:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button x:Name="MyButton"
            Height="100" 
            Width="300" 
            Content="Click" 
            FontSize="40" 
            FontWeight="Bold" 
            VerticalAlignment="Center" 
            HorizontalAlignment="Center" 
            Background="Red" Click="MyButton_Click"/>
</Grid>
<VisualStateManager.VisualStateGroups>
    <VisualStateGroup>
        <VisualState x:Name="Blue">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyButton"
                                               Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Aqua"/>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

This is the code behind:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void MyButton_Click(object sender, RoutedEventArgs e)
    {
        VisualStateManager.GoToState(this, "Blue", true);
    }
}

In theory, this should change Button-Color to "Aqua" when you click on it, but nothing happens.

+4
source share
1 answer

Put the content inside and apply VisualState to the control instead on the page. ContentControl

Also use instead to animate the color of the button. ObjectAnimation ColorAnimation

<ContentControl x:Name="contentControl">
    <ContentControl.Template>
        <ControlTemplate>
            <Grid
              Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                <Button x:Name="MyButton"
                        Height="100" 
                        Width="300" 
                        Content="Click" 
                        FontSize="40" 
                        FontWeight="Bold" 
                        VerticalAlignment="Center" 
                        HorizontalAlignment="Center" 
                        Background="Red" Click="Button_Click"/>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CustomGroups">
                        <VisualState x:Name="Blue">
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="MyButton" 
                                    Storyboard.TargetProperty="Background.Color"
                                    To="Aqua"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Grid>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

, :

VisualStateManager.GoToState(contentControl, "Blue", true);
+5

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


All Articles