如何从XAML访问元素资源中的故事板?

9

考虑以下代码:

<UserControl x:Class="MyApp.MyControl"
             ...
         xmlns:local="clr-namespace:MyApp"
         DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">

    <UserControl.Template>
        <ControlTemplate>
            <ControlTemplate.Resources>
                <Storyboard x:Key="MyStory">
                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
                        <SplineColorKeyFrame KeyTime="0:0:1" Value="Red"/>
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </ControlTemplate.Resources>

            <Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
                ...
            </Border>

            <ControlTemplate.Triggers>
                <Trigger SourceName="brdBase" Property="IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource MyStory}"/>
                    </Trigger.EnterActions>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

上面的代码没有问题。现在,我想将MyStory的关键帧值绑定到此用户控件的DP(名为SpecialColor)上,如下所示:
<Storyboard x:Key="MyStory">
    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
        <SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
    </ColorAnimationUsingKeyFrames>
</Storyboard>

会导致错误:

无法冻结此故事板时间轴树以供跨线程使用。

可以使用代码后台实现此操作,但如何仅在XAML中执行?


借助代码后台的解决方案:

步骤1:MyStory故事板放入brdBase资源中。

<UserControl.Template>
    <ControlTemplate>
        <Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
            <Border.Resources>
                <Storyboard x:Key="MyStory">
                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
                        <SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </Border.Resources>
            ...
        </Border>

        <ControlTemplate.Triggers>
            <Trigger SourceName="brdBase" Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource MyStory}"/>
                </Trigger.EnterActions>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</UserControl.Template>

错误: 找不到名为'MyStory'的资源。资源名称区分大小写。

第二步:消除IsMouseOver属性上的Trigger,并从后台代码开始MyStory

<UserControl.Template>
    <ControlTemplate>
        <Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black" MouseEnter="brdBase_MouseEnter">
            <Border.Resources>
                <Storyboard x:Key="MyStory">
                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
                        <SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </Border.Resources>
        </Border>
    </ControlTemplate>
</UserControl.Template>

C# 代码后置:

private void brdBase_MouseEnter(object sender, MouseEventArgs e)
{
   Border grdRoot = (Border)this.Template.FindName("brdBase", this);
   Storyboard story = grdRoot.Resources["MyStory"] as Storyboard;

   story.Begin(this, this.Template);
}

步骤3:解决方案已经完成,但是第一次运行时它不起作用。幸运的是,有一个解决方法来解决这个问题。只需将ControlTemplate放在Style中即可。

(我需要使用除了EventTrigger之外的其他Trigger类型,并且必须使用ControlTemplate包装UserControl元素。)


更新:

关于使用ObjectDataProvider的想法失败了。

  1. ObjectDataProvider资源不能用于提供故事板!!! 错误报告如下:
    • XamlParseException:设置属性'System.Windows.Media.Animation.BeginStoryboard.Storyboard'引发了异常。
    • InnerException:'System.Windows.Data.ObjectDataProvider'不是属性'Storyboard'的有效值。
  2. AssociatedControl DP始终为空。

以下是代码:

<UserControl.Template>
    <ControlTemplate>
        <ControlTemplate.Resources>
            <local:StoryboardFinder x:Key="StoryboardFinder1" AssociatedControl="{Binding ElementName=brdBase}"/>
            <ObjectDataProvider x:Key="dataProvider" ObjectInstance="{StaticResource StoryboardFinder1}" MethodName="Finder">
                <ObjectDataProvider.MethodParameters>
                    <sys:String>MyStory</sys:String>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </ControlTemplate.Resources>

        <Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
            <Border.Resources>
                <Storyboard x:Key="MyStory">
                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
                        <SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=SpecialColor}"/>
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </Border.Resources>
            ...
        </Border>

        <ControlTemplate.Triggers>
            <Trigger SourceName="brdBase" Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource dataProvider}"/>
                </Trigger.EnterActions>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</UserControl.Template>

故事板查找器类:
StoryboardFinder 类:
public class StoryboardFinder : DependencyObject
{
    #region ________________________________________  AssociatedControl

    public Control AssociatedControl
    {
        get { return (Control)GetValue(AssociatedControlProperty); }
        set { SetValue(AssociatedControlProperty, value); }
    }

    public static readonly DependencyProperty AssociatedControlProperty =
        DependencyProperty.Register("AssociatedControl",
                                    typeof(Control),
                                    typeof(StoryboardFinder),
                                    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None));

    #endregion

    public Storyboard Finder(string resourceName)
    {
        //
        // Associated control is always null :(
        //
        return new Storyboard();
    }
}
2个回答

4
假如这段代码是真的呢?
<UserControl x:Class="MyApp.MyControl"
             ...
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:l="clr-namespace:MyApp"
             DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">

    <UserControl.Resources>
        <Style TargetType="{x:Type l:MyControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type l:MyControl}">
                        <Border x:Name="brdBase" BorderThickness="1" BorderBrush="Cyan" Background="Black">
                            <Border.Resources>
                                <Storyboard x:Key="MyStory">
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
                                        <SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type l:MyControl}}, Path=SpecialColor}"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </Border.Resources>

                            <i:Interaction.Triggers>
                                <l:InteractiveTrigger Property="IsMouseOver" Value="True">
                                    <l:InteractiveTrigger.CommonActions>
                                        <BeginStoryboard Storyboard="{StaticResource MyStory}"/>
                                    </l:InteractiveTrigger.CommonActions>
                                </l:InteractiveTrigger>
                            </i:Interaction.Triggers>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
</UserControl>

如果是这样的话,我可以在IsMouseOver属性上设置触发器... 我很高兴地说,这段代码是可行的 :) 我只能在<Border.Triggers>标签中使用EventTrigger。这是个限制。所以我开始思考这个想法:如果我能够有一个自定义的触发器,可以在FrameworkElement.Triggers范围内工作,那该怎么办呢?下面是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;

namespace TriggerTest
{
    /// <summary>
    /// InteractiveTrigger is a trigger that can be used as the System.Windows.Trigger but in the System.Windows.Interactivity.
    /// <para>
    /// Note: There is neither `EnterActions` nor `ExitActions` in this class. The `CommonActions` can be used instead of `EnterActions`.
    /// Also, the `Actions` property which is of type System.Windows.Interactivity.TriggerAction can be used.
    /// </para>
    /// <para> </para>
    /// <para>
    /// There is only one kind of triggers (i.e. EventTrigger) in the System.Windows.Interactivity. So you can use the following triggers in this namespace:
    /// <para>1- InteractiveTrigger : Trigger</para>
    /// <para>2- InteractiveMultiTrigger : MultiTrigger</para>
    /// <para>3- InteractiveDataTrigger : DataTrigger</para>
    /// <para>4- InteractiveMultiDataTrigger : MultiDataTrigger</para>
    /// </para>
    /// </summary>
    public class InteractiveTrigger : TriggerBase<FrameworkElement>
    {
        #region ___________________________________________________________________________________  Properties

        #region ________________________________________  Value

        /// <summary>
        /// [Wrapper property for ValueProperty]
        /// <para>
        /// Gets or sets the value to be compared with the property value of the element. The comparison is a reference equality check.
        /// </para>
        /// </summary>
        public object Value
        {
            get { return (object)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value",
                                        typeof(object),
                                        typeof(InteractiveTrigger),
                                        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None, OnValuePropertyChanged));

        private static void OnValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            InteractiveTrigger instance = sender as InteractiveTrigger;

            if (instance != null)
            {
                if (instance.CanFire)
                    instance.Fire();
            }
        }

        #endregion


        /// <summary>
        /// Gets or sets the name of the object with the property that causes the associated setters to be applied.
        /// </summary>
        public string SourceName
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the property that returns the value that is compared with this trigger.Value property. The comparison is a reference equality check.
        /// </summary>
        public DependencyProperty Property
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a collection of System.Windows.Setter objects, which describe the property values to apply when the trigger object becomes active.
        /// </summary>
        public List<Setter> Setters
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the collection of System.Windows.TriggerAction objects to apply when this trigger object becomes active.
        /// </summary>
        public List<System.Windows.TriggerAction> CommonActions
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a value indicating whether this trigger can be active to apply setters and actions.
        /// </summary>
        private bool CanFire
        {
            get
            {
                if (this.AssociatedObject == null)
                {
                    return false;
                }
                else
                {
                    object associatedValue;

                    if (string.IsNullOrEmpty(SourceName))
                        associatedValue = this.AssociatedObject.GetValue(Property);
                    else
                        associatedValue = (this.AssociatedObject.FindName(SourceName) as DependencyObject).GetValue(Property);

                    TypeConverter typeConverter = TypeDescriptor.GetConverter(Property.PropertyType);
                    object realValue = typeConverter.ConvertFromString(Value.ToString());

                    return associatedValue.Equals(realValue);
                }
            }
        }

        #endregion


        #region ___________________________________________________________________________________  Methods

        /// <summary>
        /// Fires (activates) current trigger by setting setter values and invoking all actions.
        /// </summary>
        private void Fire()
        {
            //
            // Setting setters values to their associated properties..
            //
            foreach (Setter setter in Setters)
            {
                if (string.IsNullOrEmpty(setter.TargetName))
                    this.AssociatedObject.SetValue(setter.Property, setter.Value);
                else
                    (this.AssociatedObject.FindName(setter.TargetName) as DependencyObject).SetValue(setter.Property, setter.Value);
            }

            //
            // Firing actions.. 
            //
            foreach (System.Windows.TriggerAction action in CommonActions)
            {
                Type actionType = action.GetType();

                if (actionType == typeof(BeginStoryboard))
                {
                    (action as BeginStoryboard).Storyboard.Begin();
                }
                else
                    throw new NotImplementedException();
            }

            this.InvokeActions(null);
        }

        #endregion


        #region ___________________________________________________________________________________  Events

        public InteractiveTrigger()
        {
            Setters = new List<Setter>();
            CommonActions = new List<System.Windows.TriggerAction>();
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            if (Property != null)
            {
                object propertyAssociatedObject;

                if (string.IsNullOrEmpty(SourceName))
                    propertyAssociatedObject = this.AssociatedObject;
                else
                    propertyAssociatedObject = this.AssociatedObject.FindName(SourceName);

                //
                // Adding a property changed listener to the property associated-object..
                //
                DependencyPropertyDescriptor dpDescriptor = DependencyPropertyDescriptor.FromProperty(Property, propertyAssociatedObject.GetType());
                dpDescriptor.AddValueChanged(propertyAssociatedObject, PropertyListener_ValueChanged);
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            if (Property != null)
            {
                object propertyAssociatedObject;

                if (string.IsNullOrEmpty(SourceName))
                    propertyAssociatedObject = this.AssociatedObject;
                else
                    propertyAssociatedObject = this.AssociatedObject.FindName(SourceName);

                //
                // Removing previously added property changed listener from the associated-object..
                //
                DependencyPropertyDescriptor dpDescriptor = DependencyPropertyDescriptor.FromProperty(Property, propertyAssociatedObject.GetType());
                dpDescriptor.RemoveValueChanged(propertyAssociatedObject, PropertyListener_ValueChanged);
            }
        }

        private void PropertyListener_ValueChanged(object sender, EventArgs e)
        {
            if (CanFire)
                Fire();
        }

        #endregion
    }
}

我还创建了其他触发器类型(例如InteractiveMultiTriggerInteractiveDataTriggerInteractiveMultiDataTrigger),以及一些更多的操作,这使得有条件和多条件的EventTriggers成为可能。如果您专业的人士确认此解决方案,我将发布它们所有。

感谢您的关注!


很好,它应该可以无问题地工作。我唯一能看到的是,它可能不太高效,因为您不再冻结故事板时间轴,这意味着它不使用多个线程。 - Erti-Chris Eelmaa
你说得对。我正在寻找一种调用TriggerActionInvoke方法的方式。通过这种方式,每个必需的东西都应该在内部完成。你有任何想法吗?(我将其标记为“无法使用此片段”)。 - Mehdi
从文档的另一个视角来看,Begin() 实际上会自动冻结。我更感兴趣的是为什么 Xaml 不起作用?你为什么要尝试访问内部内容呢?除非绝对没有其他方法,否则这永远不是一个好主意。 - Erti-Chris Eelmaa
我尝试在开始之前通过Storyboard.Freeze()方法冻结Storyboard,但是它导致了这个错误:This Freezable cannot be frozen. 尽管我可以使用Storyboard.GetAsFrozen()方法创建一个冻结的副本,然后开始它。我真的不知道这个操作是否会使代码更有效率。它会吗?我阅读了_Storyboard.cs_的.Net源代码。如果您调用Begin()方法,它将使用SnapshotAndReplace作为其交接行为。 - Mehdi
我同意你的看法,我不应该使用内部的东西。对我来说效率很重要,因为我计划在我的应用程序中广泛使用InteractiveTrigger(以及它的MultiTrigger、DataTrigger和MultiDataTrigger版本)。 - Mehdi
标记为答案。但是欢迎提出任何改进其效率的建议。 - Mehdi

3

嗯,你不能真正地绑定“To”或“From”,因为故事板必须被冻结,才能有效地处理跨线程。

解决方案1) 最简单的解决方案,不需要任何hack(涉及代码后台): 添加MouseOver事件处理程序,在事件处理程序中,找到必要的动画,直接设置“To”属性,这样就不会使用绑定,也可以进行“冻结”。这样你就不会硬编码任何东西:)。

解决方案2) 有一个很酷的hack,只支持XAML(当然还有一点转换器魔力),但我不建议使用它。不过它仍然很酷:) WPF animation: binding to the "To" attribute of storyboard animation 参见Jason的答案。

还有几件事情可以尝试:

解决方案3) 不要使用依赖属性,而是实现INotifyPropertyChanged。这样你仍然可以绑定“To”。请注意,我认为这在理论上应该可行,但我没有尝试过。

解决方案4) 将Mode=OneTime应用于你的绑定。也许它会起作用?

解决方案5) 编写自己的附加行为,以在正确的线程上评估依赖属性并设置“To”属性。我认为这将是一个不错的解决方案。

这里还有一个很好的重复问题:WPF Animation "Cannot freeze this Storyboard timeline tree for use across threads"


感谢@Erti-Chris Eelmaa。第一个解决方案并不是预期的(我已经提供了完整版本!)。此外,我不同意使用“Tag”属性,因为它会有性能限制。 - Mehdi
我有一个想法。☼我们可以使用ObjectDataProvider来调用返回元素任意资源的静态方法吗? - Mehdi
我已经测试了第3和第4个解决方案,但都没有成功。 - Mehdi
Mimi,你尝试了什么,第5个解决方案出现了什么错误?我不能提供完整的实现,因为我没有那么多空闲时间。有一个新的附加属性BindableValue,你可以设置它,它将控制“值”属性。 - Erti-Chris Eelmaa
+1 感谢您的关注。我已经找到了一个解决方案,目前正在尝试完成和测试它。我使用 System.Windows.Interactivity 创建了一个新的 Trigger,以便在通常不可用的范围内使用它(即除了 TemplateStyle 之外的任何地方)。通过这种方式,我可以实现使用动态故事板的主要目标。到目前为止,我已成功创建了 InteractiveTriggerConditionalEventTriggerMultiConditionalEventTrigger。它们已经通过了初步测试。我现在正在尝试创建 InteractiveDataTriggerInteractiveMultiDataTrigger - Mehdi
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接