0
我有WPF C#應用程序與用戶控件我想在3個狀態之間動畫。WPF屬性觸發器與枚舉
用戶控制:
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其中I定義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>
然後,我有一個xaml綁定到屬性Layout,它觸發屬性值的更改。
問題是動畫並不總是適用於每個狀態更改。我見過的所有例子都是使用Enter和ExitActions的布爾屬性,但在這裏我有3個可能不同的例子。
有沒有一個很好的方法來完成這項工作?
感謝