C# VisualStateManager WPF不工作

C# VisualStateManager WPF不工作,c#,wpf,xaml,C#,Wpf,Xaml,我专门在VS Blend 2017 designer中设计了一个窗口,没有对XAML代码进行任何更改。我创建了一个状态组和一个状态,并在按钮上记录了更改。当尝试从代码隐藏应用状态时,不会发生任何事情 我也看到了关于同一个问题的主题,但不幸的是没有一个对我有用 代码隐藏: VisualStateManager.GoToState(this, "State1", false); XAML: 谢谢你的帮助 我认为应该使用GoToElementState而不是GoToState 请试试这个 将名称

我专门在VS Blend 2017 designer中设计了一个窗口,没有对XAML代码进行任何更改。我创建了一个状态组和一个状态,并在按钮上记录了更改。当尝试从代码隐藏应用状态时,不会发生任何事情

我也看到了关于同一个问题的主题,但不幸的是没有一个对我有用

代码隐藏:

VisualStateManager.GoToState(this, "State1", false);
XAML:



谢谢你的帮助

我认为应该使用GoToElementState而不是GoToState

请试试这个

将名称添加到网格:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="MyGrid">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="MyStates">
                <VisualState x:Name="State1">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button">
                            <EasingColorKeyFrame KeyTime="0" Value="Red"/>
                        </ColorAnimationUsingKeyFrames>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button">
                            <EasingColorKeyFrame KeyTime="0" Value="Blue"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Button x:Name="button" Content="Button" Margin="208.306,116.798,218.093,0" VerticalAlignment="Top" Height="26.065" Click="button_Click"/>

    </Grid>
</Window>

并在代码隐藏中使用它

        /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToElementState(this.MyGrid, "State1", false);
        }
    }
}
//
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
VisualStateManager.GoToElementState(this.MyGrid,“State1”,false);
}
}
}

视觉状态应在控件的
控件模板中定义:

<Window x:Class="Wpf_2017.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Window10" Height="300" Width="300">
    <Window.Template>
        <ControlTemplate TargetType="{x:Type Window}">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="MyStates">
                        <VisualState x:Name="State1">
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button">
                                    <EasingColorKeyFrame KeyTime="0" Value="#FFEA8686"/>
                                </ColorAnimationUsingKeyFrames>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button">
                                    <EasingColorKeyFrame KeyTime="0" Value="#FFFF4848"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <AdornerDecorator>
                    <StackPanel>
                        <Button x:Name="button" Content="Button" Margin="208.306,116.798,218.093,0" VerticalAlignment="Top" Height="26.065" Click="button_Click"/>
                    </StackPanel>
                </AdornerDecorator>
            </Border>
        </ControlTemplate>
    </Window.Template>
</Window>


这就是VisualStateManager查找它们的地方。

嗨,很抱歉。我会很快给你正确的答案。谢谢你,但没有任何改变!这对我来说确实很好。你真的复制了我的XAML标记并按下了按钮吗?很抱歉,现在它成功了!你知道为什么设计师会产生错误的代码吗?嗯,它根本不足以处理所有情况。毕竟,它是一个设计师:)
<Window x:Class="Wpf_2017.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Window10" Height="300" Width="300">
    <Window.Template>
        <ControlTemplate TargetType="{x:Type Window}">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="MyStates">
                        <VisualState x:Name="State1">
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button">
                                    <EasingColorKeyFrame KeyTime="0" Value="#FFEA8686"/>
                                </ColorAnimationUsingKeyFrames>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button">
                                    <EasingColorKeyFrame KeyTime="0" Value="#FFFF4848"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <AdornerDecorator>
                    <StackPanel>
                        <Button x:Name="button" Content="Button" Margin="208.306,116.798,218.093,0" VerticalAlignment="Top" Height="26.065" Click="button_Click"/>
                    </StackPanel>
                </AdornerDecorator>
            </Border>
        </ControlTemplate>
    </Window.Template>
</Window>