Set custom attached property on StoryBoard

筅森魡賤 提交于 2019-12-01 05:35:54

问题


I have a storyboard and would like to set the attached property VisualStateUtility.InitialState. I've tried various combinations, but the property never gets resolved.

I get the following error: Cannot resolve TargetProperty (VisualStateUtility.InitialState)

How can I set the value of my custom attached property on the Storyboard?

<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="(Fully.Qualified.Namespace.VisualStateUtility.InitialState)"  Storyboard.TargetName="ExpanderButton">

    public static string GetInitialState(DependencyObject obj)
    {
        return (string)obj.GetValue(InitialStateProperty);
    }

    public static void SetInitialState(DependencyObject obj, string value)
    {
        obj.SetValue(InitialStateProperty, value);
    }

    // Using a DependencyProperty as the backing store for InitialState.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty InitialStateProperty =
        DependencyProperty.RegisterAttached("InitialState", typeof(string), typeof(VisualStateUtility), new PropertyMetadata(null,OnInitialStatePropertyChanged));

回答1:


This should do the trick

<ObjectAnimationUsingKeyFrames x:Name="animation" Duration="0" Storyboard.TargetProperty="xmlnsAlias:VisualStateUtility.InitialState"  Storyboard.TargetName="ExpanderButton">

Notice how a name is added to the animation, the parentheses are removed from the target property name, which is then prefixed with the xmlns alias from the xaml header.

In your code behind you'll have to add this:

InitializeComponent();
Storyboard.SetTargetProperty(animation, new PropertyPath(Fully.Qualified.Namespace.VisualStateUtility.InitialState));

Apparently this last step is required for animating custom attached properties. A real pain if you'd ask me.




回答2:


I ran into this:

http://forums.silverlight.net/t/182227.aspx

Which another user says it's un-supported.

Frank Lan Writes:

Hi, It's a known issue. And the workaround seems to be: animate the custom attached property from code instead of xaml. Sorry for any inconvenience caused by the issue. Frank Lan Microsoft Online Community Support



来源:https://stackoverflow.com/questions/7520709/set-custom-attached-property-on-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!