0

我有一个带有用户控件的 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 个可能会有所不同。

有什么好方法可以使这项工作吗?

谢谢

4

1 回答 1

0

我已经能够使用动画无法与数据触发器正常工作的解决方案来修复它

这个想法是在 EnterActions 中定义 Storyboard,在 BeginStoryboard之前,确保删除所有其他 Storyboard。

固定代码是这样的:(请注意 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>
于 2013-05-03T20:30:39.197 回答