WPF使用VisualStateManager在&;中设置面板动画;出来
我希望我尝试的是使用视觉状态 我想做的是有一个按钮,可以在两种状态之间切换stackPanel:“In”和“Out”。然后,我会在按钮的单击事件上调用VisualStateManager.GoToState,在这两种状态之间切换 我遇到的问题是我不知道如何将状态附加到StackPanel。它不允许我使用表达式混合。所以我被卡住了。。。是否仍然可以使用VisualStateManager设置此面板的进出动画?(我知道我可以使用情节串连板,创建和输入输出动画,但如果可能的话,我更倾向于使用状态。) 我真的希望这是可能的。否则,VisualStateManager除了在按钮上执行噱头翻滚效果之外还有什么好处WPF使用VisualStateManager在&;中设置面板动画;出来,wpf,silverlight,expression-blend,visualstatemanager,Wpf,Silverlight,Expression Blend,Visualstatemanager,我希望我尝试的是使用视觉状态 我想做的是有一个按钮,可以在两种状态之间切换stackPanel:“In”和“Out”。然后,我会在按钮的单击事件上调用VisualStateManager.GoToState,在这两种状态之间切换 我遇到的问题是我不知道如何将状态附加到StackPanel。它不允许我使用表达式混合。所以我被卡住了。。。是否仍然可以使用VisualStateManager设置此面板的进出动画?(我知道我可以使用情节串连板,创建和输入输出动画,但如果可能的话,我更倾向于使用状态。)
谢谢你的帮助 刚刚启动了Blend并得到了以下结果:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<StackPanel x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="PanelState">
<VisualState x:Name="In"/>
<VisualState x:Name="Out">
<Storyboard>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="stackPanel">
<EasingThicknessKeyFrame KeyTime="0" Value="-65,15,0,0"/>
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<i:Interaction.Behaviors>
<ei:DataStateBehavior Binding="{Binding IsChecked, ElementName=toggleButton}" Value="True" TrueState="In" FalseState="Out"/>
</i:Interaction.Behaviors>
<Button Content="Button" Width="50" HorizontalAlignment="Left" Click="Button_Click"/>
<StackPanel x:Name="stackPanel" Height="100" HorizontalAlignment="Left" Margin="0,15,0,0">
<TextBlock><Run Text="Funnytext"/></TextBlock>
</StackPanel>
<ToggleButton x:Name="toggleButton" Content="Toggle" Width="50" HorizontalAlignment="Left"/>
</StackPanel>
</Window>
(不知道你的意思,所以我只包括了两个)
编辑:我的错,没有注意到我没有包含clickhandler。为方便起见,我提供了一个使用Togglebutton切换状态的示例 您甚至没有向按钮添加事件处理程序。。。你想给我看什么?我的问题是,使用您现有的代码,如何将状态从out更改为in?在你的代码中,你把什么参数传递给VSM?那太好了。谢谢因此,WPF似乎最容易使用GoToElementState,而不是GoToState,因为可视状态应用于LayoutRoot。(很抱歉延迟,周末无法访问)我还尝试将VisualStateGroup添加到窗口的LayoutRoot中,然后在单击事件中将“this”传递到VSM中(例如:VisualStateManager.GoToState(这是“in”,true)…但这对我也不起作用。奇怪的是…我只是把相同的代码放进了WinPhone7项目中,而且效果很好…为什么它对WinPhone7 Silverlight有效,但对WPF无效?
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
var sgs=VisualStateManager.GetVisualStateGroups(LayoutRoot);
var sg=sgs[0] as VisualStateGroup;
VisualStateManager.GoToElementState(LayoutRoot, ((VisualState) sg.States[sg.CurrentState == sg.States[0]?1:0]).Name,true);
}