剛剛解僱了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>
和後面的代碼:
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);
}
(不知道是什麼的StackPanel你的意思,所以我只包括兩名)
編輯:我的壞,沒有注意到我沒有包括點擊處理程序。爲了您的方便,我包含了一個使用Togglebutton切換狀態的示例...
我也嘗試將VisualStateGroup添加到窗口的LayoutRoot中,然後在Click事件中將'this'傳入VSM中(例如:VisualStateManager.GoToState(this,「In」,true)...但是這對我也不適用。 – 2011-02-24 21:25:32
奇怪...我只是把相同的代碼放入WinPhone7項目中,並且它工作正常。 。爲什麼它可以用於WinPhone7 Silverlight,但不是WPF? – 2011-02-24 21:27:17