How to access a storyboard within an element resources from XAML?

前端 未结 2 1342
迷失自我
迷失自我 2021-01-12 00:29

Consider this code:



        
2条回答
  •  攒了一身酷
    2021-01-12 01:22

    Well, you can't really bind to "To" nor From, because the storyboard has to be frozen, in order to work efficiently with cross-threading.

    Solution1) Simplest solution without hacks(involves code-behind): Add MouseOver event handler & in the event handler, locate necessary animation, set the "To" property directly, so you won't use binding and the "freezing" can be done. This way you won't hardcode anything :).

    Solution2) There is a cool hack that supports XAML only( a little bit of converter magic ofcourse ), but I do not suggest it. It's cool nonetheless :) WPF animation: binding to the "To" attribute of storyboard animation See answer by Jason.

    There are few things more that you can try:

    Solution3) Don't use Dependency properties, but rather implement INotifyProperthChanged. This way you still can BIND "To". Note that I think this should theoretically work, but I have not tried.

    Solution4) Apply Mode=OneTime to your binding. Maybe it works?

    Solution5) Write your own attached behavior that will evaluate dependency property on correct thread and set "To" property. I think that will be nice solution.

    Here is good duplicate too: WPF Animation "Cannot freeze this Storyboard timeline tree for use across threads"

提交回复
热议问题