2013-05-02 133 views
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個可能不同的例子。

有沒有一個很好的方法來完成這項工作?

感謝

回答

0

我已經能夠使用該解決方案修復它爲Animation not working correctly with data triggers

的想法是在EnterActions定義故事板,並的BeginStoryboard,確保每一個其他故事板除去。

固定代碼是這樣的:(請注意s:S.Cart ......它們是在另一個類中保存的常量)。

<Style TargetType="c:Cart"> 
    <Setter Property="Margin" Value="{x:Static s:S+Cart.Hidden}" /> 
    <Style.Triggers> 
     <Trigger Property="Layout" Value="Visible"> 
      <Trigger.EnterActions> 
       <RemoveStoryboard BeginStoryboardName="CartCollapsed" /> 
       <RemoveStoryboard BeginStoryboardName="CartHidden" /> 
       <BeginStoryboard Name="CartVisible"> 
        <Storyboard> 
         <ThicknessAnimation Storyboard.TargetProperty="Margin" 
            Duration="0:0:0.5" 
            To="{x:Static s:S+Cart.Visible}" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.EnterActions> 
     </Trigger> 
     <Trigger Property="Layout" Value="Collapsed"> 
      <Trigger.EnterActions> 
       <RemoveStoryboard BeginStoryboardName="CartVisible" /> 
       <RemoveStoryboard BeginStoryboardName="CartHidden" /> 
       <BeginStoryboard Name="CartCollapsed"> 
        <Storyboard> 
         <ThicknessAnimation Storyboard.TargetProperty="Margin" 
            Duration="0:0:0.5" 
            To="{x:Static s:S+Cart.Collapsed}" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.EnterActions> 
     </Trigger> 
     <Trigger Property="Layout" Value="Hidden"> 
      <Trigger.EnterActions> 
       <RemoveStoryboard BeginStoryboardName="CartVisible" /> 
       <RemoveStoryboard BeginStoryboardName="CartCollapsed" /> 
       <BeginStoryboard Name="CartHidden"> 
        <Storyboard> 
         <ThicknessAnimation Storyboard.TargetProperty="Margin" 
            Duration="0:0:0.5" 
            To="{x:Static s:S+Cart.Hidden}" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.EnterActions> 
     </Trigger> 
    </Style.Triggers>