我使用Blend for Visual Studio创建了最简单的WPF应用程序。
我为单个文本框创建了最简单的可视状态组:
我的问题是我无法使用此代码动态GoToState:
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>
答案 0 :(得分:1)
您的VisualStateGroups
未在TextBox
中定义,因此呼叫
var states = VisualStateManager.GetVisualStateGroups(textBox);
不会返回您的期望。如果您想获得已定义的状态,请传递包含FrameworkElement
的{{1}}(我假设此处为VisualStateGroups
):
Window
同样应该用于调用var states = VisualStateManager.GetVisualStateGroups(this);
:
VisualStateManager.GoToState