我不能在WPF中动态地停止。怎么了?

我不能在WPF中动态地停止。怎么了?,wpf,blend,visualstatemanager,Wpf,Blend,Visualstatemanager,我使用Blend for Visual Studio创建了最简单的WPF应用程序 我为一个文本框创建了最简单的可视状态组: 我的问题是,我无法使用以下代码动态地停止: public MainWindow() { InitializeComponent(); textBox.MouseEnter+=new System.Windows.Input.MouseEventHandler(textBox_MouseEnter); } private void textBox_MouseE

我使用Blend for Visual Studio创建了最简单的WPF应用程序

我为一个文本框创建了最简单的可视状态组:

我的问题是,我无法使用以下代码动态地停止:

public MainWindow()
{
    InitializeComponent();
    textBox.MouseEnter+=new System.Windows.Input.MouseEventHandler(textBox_MouseEnter);
}
private void textBox_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    // This will be an empty list
    var states = VisualStateManager.GetVisualStateGroups(textBox);
    // This will be false
    bool thisReturnsFalse =   VisualStateManager.GoToState(textBox, "VisualState1", true);
}
这是我的XAML:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="VisualStateGroup">
        <VisualState x:Name="VisualState">
            <Storyboard>
                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="textBox">
                    <EasingColorKeyFrame KeyTime="0" Value="#FFE40404"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
        <VisualState x:Name="VisualState1">
            <Storyboard>
                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="textBox">
                    <EasingColorKeyFrame KeyTime="0" Value="#FF23FF00"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyUp">
            <ei:GoToStateAction StateName="VisualState1"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="KeyDown">
            <ei:GoToStateAction StateName="VisualState"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

您的
VisualStateGroup
未在
文本框中定义,因此调用

var states = VisualStateManager.GetVisualStateGroups(textBox);
不会回报你所期望的。如果要获取已定义的状态,请传递包含
VisualStateGroups
FrameworkElement
(我假设这里是
窗口
):

调用
VisualStateManager.gostate
,可能也应该使用相同的方法:

bool shouldReturnTrue = VisualStateManager.GoToState(this, "VisualState1", true);

我对GoToState的要求来自一个事件。我必须在我的XAML上移动文本框中的VisualStateGroups,现在我得到:/*这是预期的1:/var stateGroups=VisualStateManager.GetVisualStateGroups(TextBox);/这仍然是错误的:/bool thisReturnsFalse=VisualStateManager.goState(这是“VisualState1”,错误);/另外,我的KeyUp、KeyDown也不起作用*/@profimedica更新您的XAML以反映这些更改,因为您问题中的XAML没有显示这些更改。@profimedica尝试调用GoToElementState而不是GoToState当我在原始代码中用网格名称替换“this”并在该网格上使用GoToElementState时,它会起作用。它还可以使用按钮名称并在按钮中定义VisualStateGroup。其他尝试都没有成功。现在,我需要在模板上使用GoToElementState。在我看来,这个函数在下一个版本中将有更好的实现。
bool shouldReturnTrue = VisualStateManager.GoToState(this, "VisualState1", true);