无法使用代码隐藏在 WPF 中为堆栈面板设置动画
Unable to animate stack panel in WPF using code behind
我正在尝试构建一个简单的示例来为网格上的堆栈面板设置动画。我已将边距向左偏移并尝试从后面的代码设置动画,但我无法让堆栈面板滑到屏幕上。
Xaml:
<Grid x:Name="MainContainerGrid">
<Grid x:Name="MainContent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid>
<Canvas Background="Purple" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Content="Show Left" Click="Button_Click" Width="200" Height="50" Background="Green" BorderThickness="0"/>
</Grid>
</Grid>
<StackPanel x:Name="StackPanelMenu" Width="300" HorizontalAlignment="Left" Margin="-300 0 0 0" Background="Red">
</StackPanel>
</Grid>
后面的代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
EasingDoubleKeyFrame sp_dbkf1 = new EasingDoubleKeyFrame();
EasingDoubleKeyFrame sp_dbkf2 = new EasingDoubleKeyFrame();
sp_dbkf1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
sp_dbkf1.Value = 0;
sp_dbkf2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));
sp_dbkf2.Value = 300;
DoubleAnimationUsingKeyFrames sp_db = new DoubleAnimationUsingKeyFrames();
sp_db.KeyFrames.Add(sp_dbkf1);
sp_db.KeyFrames.Add(sp_dbkf2);
Storyboard.SetTargetProperty(sp_db, new PropertyPath(TranslateTransform.XProperty));
Storyboard.SetTargetName(sp_db, StackPanelMenu.Name);
Storyboard sb = new Storyboard();
sb.Children.Add(sp_db);
sb.Begin(StackPanelMenu);
}
对此有什么想法吗?如果您有有助于此特定示例的资源,也请分享。
感谢 Clemens 的建议,这里是对 xaml 和隐藏代码的更改。
<StackPanel x:Name="StackPanelMenu" Width="300" HorizontalAlignment="Left" Margin="-300 0 0 0" Background="Red">
<StackPanel.RenderTransform>
<TransformGroup>
<TranslateTransform x:Name="StackPanelMenu_TranslateTransform" />
</TransformGroup>
</StackPanel.RenderTransform>
</StackPanel>
.
private void Button_Click(object sender, RoutedEventArgs e)
{
// Set the animation targets
// <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
// Storyboard.TargetName = "CloseButtonIcon" >
// DoubleAnimation db = new DoubleAnimation();
//Storyboard.SetTarget(db, LeftMenu.RenderTransform);
//Storyboard.SetTargetProperty(db, new PropertyPath("X"));
EasingDoubleKeyFrame sp_dbkf1 = new EasingDoubleKeyFrame();
EasingDoubleKeyFrame sp_dbkf2 = new EasingDoubleKeyFrame();
sp_dbkf1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
sp_dbkf1.Value = 0;
sp_dbkf2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));
sp_dbkf2.Value = 300;
DoubleAnimationUsingKeyFrames sp_db = new DoubleAnimationUsingKeyFrames();
sp_db.KeyFrames.Add(sp_dbkf1);
sp_db.KeyFrames.Add(sp_dbkf2);
Storyboard.SetTargetName(sp_db, "StackPanelMenu_TranslateTransform");
StackPanelMenu_TranslateTransform.BeginAnimation(TranslateTransform.XProperty, sp_db);
}
我正在尝试构建一个简单的示例来为网格上的堆栈面板设置动画。我已将边距向左偏移并尝试从后面的代码设置动画,但我无法让堆栈面板滑到屏幕上。
Xaml:
<Grid x:Name="MainContainerGrid">
<Grid x:Name="MainContent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid>
<Canvas Background="Purple" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Content="Show Left" Click="Button_Click" Width="200" Height="50" Background="Green" BorderThickness="0"/>
</Grid>
</Grid>
<StackPanel x:Name="StackPanelMenu" Width="300" HorizontalAlignment="Left" Margin="-300 0 0 0" Background="Red">
</StackPanel>
</Grid>
后面的代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
EasingDoubleKeyFrame sp_dbkf1 = new EasingDoubleKeyFrame();
EasingDoubleKeyFrame sp_dbkf2 = new EasingDoubleKeyFrame();
sp_dbkf1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
sp_dbkf1.Value = 0;
sp_dbkf2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));
sp_dbkf2.Value = 300;
DoubleAnimationUsingKeyFrames sp_db = new DoubleAnimationUsingKeyFrames();
sp_db.KeyFrames.Add(sp_dbkf1);
sp_db.KeyFrames.Add(sp_dbkf2);
Storyboard.SetTargetProperty(sp_db, new PropertyPath(TranslateTransform.XProperty));
Storyboard.SetTargetName(sp_db, StackPanelMenu.Name);
Storyboard sb = new Storyboard();
sb.Children.Add(sp_db);
sb.Begin(StackPanelMenu);
}
对此有什么想法吗?如果您有有助于此特定示例的资源,也请分享。
感谢 Clemens 的建议,这里是对 xaml 和隐藏代码的更改。
<StackPanel x:Name="StackPanelMenu" Width="300" HorizontalAlignment="Left" Margin="-300 0 0 0" Background="Red">
<StackPanel.RenderTransform>
<TransformGroup>
<TranslateTransform x:Name="StackPanelMenu_TranslateTransform" />
</TransformGroup>
</StackPanel.RenderTransform>
</StackPanel>
.
private void Button_Click(object sender, RoutedEventArgs e)
{
// Set the animation targets
// <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
// Storyboard.TargetName = "CloseButtonIcon" >
// DoubleAnimation db = new DoubleAnimation();
//Storyboard.SetTarget(db, LeftMenu.RenderTransform);
//Storyboard.SetTargetProperty(db, new PropertyPath("X"));
EasingDoubleKeyFrame sp_dbkf1 = new EasingDoubleKeyFrame();
EasingDoubleKeyFrame sp_dbkf2 = new EasingDoubleKeyFrame();
sp_dbkf1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
sp_dbkf1.Value = 0;
sp_dbkf2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5));
sp_dbkf2.Value = 300;
DoubleAnimationUsingKeyFrames sp_db = new DoubleAnimationUsingKeyFrames();
sp_db.KeyFrames.Add(sp_dbkf1);
sp_db.KeyFrames.Add(sp_dbkf2);
Storyboard.SetTargetName(sp_db, "StackPanelMenu_TranslateTransform");
StackPanelMenu_TranslateTransform.BeginAnimation(TranslateTransform.XProperty, sp_db);
}