我有一个带有用户控件的 WPF C# 应用程序,我想在 3 个状态之间进行动画处理。
用户控制:
public partial class Cart : UserControl
{
/// <summary>
/// The <see cref="Layout" /> dependency property's name.
/// </summary>
public const string LayoutPropertyName = "Layout";
/// <summary>
/// Gets or sets the value of the <see cref="Layout" />
/// property. This is a dependency property.
/// </summary>
public Visibility Layout
{
get
{
return (Visibility)GetValue(LayoutProperty);
}
set
{
SetValue(LayoutProperty, value);
}
}
public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register(
LayoutPropertyName,
typeof(Visibility),
typeof(Carrito),
new PropertyMetadata(Visibility.Hidden));
}
另外,我有一个 Styles.xaml,我在其中定义了 3 个故事板,我计划使用属性触发器重用这些故事板,以将控件定位在不同的位置。
<Storyboard x:Key="CartVisible">
<ThicknessAnimation Storyboard.TargetProperty="Margin" Duration="0:0:0.5" To="0,648,0,0" />
</Storyboard>
<Storyboard x:Key="CartCollapsed">
<ThicknessAnimation Storyboard.TargetProperty="Margin" Duration="0:0:0.5" To="0,736,0,0" />
</Storyboard>
<Storyboard x:Key="CartHidden">
<ThicknessAnimation Storyboard.TargetProperty="Margin" Duration="0:0:0.5" To="0,768,0,0" />
</Storyboard>
<Style TargetType="c:Cart" TargetType="FrameworkElement">
<Setter Property="Margin" Value="0,768,0,0" />
<Style.Triggers>
<Trigger Property="Layout" Value="Visible">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource CartVisible}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource CartHidden}" />
</Trigger.ExitActions>
</Trigger>
<Trigger Property="Layout" Value="Collapsed">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource CartCollapsed}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource CartVisible}" />
</Trigger.ExitActions>
</Trigger>
<Trigger Property="Layout" Value="Hidden">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource CartHidden}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource CartVisible}" />
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
然后,我有一个到属性 Layout 的 xaml 绑定,它会触发属性值的更改。
问题是动画并不总是适用于每个状态变化。我看到的所有示例都是针对使用 Enter 和 ExitActions 的 bool 属性,但这里我有 3 个可能会有所不同。
有什么好方法可以使这项工作吗?
谢谢