Note that there are some explanatory texts on larger screens.

plurals
  1. POBind to an attached behavior on a Storyboard
    text
    copied!<p>I have created an attached dependency property for Storyboards, with the intention of enabling me to call a method on my ViewModel when a Storyboard Completed event fires:</p> <pre><code>public static class StoryboardExtensions { public static ICommand GetCompletedCommand(DependencyObject target) { return (ICommand)target.GetValue(CompletedCommandProperty); } public static void SetCompletedCommand(DependencyObject target, ICommand value) { target.SetValue(CompletedCommandProperty, value); } public static readonly DependencyProperty CompletedCommandProperty = DependencyProperty.RegisterAttached( "CompletedCommand", typeof(ICommand), typeof(StoryboardExtensions), new FrameworkPropertyMetadata(null, OnCompletedCommandChanged)); static void OnCompletedCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { Storyboard storyboard = target as Storyboard; if (storyboard == null) throw new InvalidOperationException("This behavior can be attached to Storyboard item only."); storyboard.Completed += new EventHandler(OnStoryboardCompleted); } static void OnStoryboardCompleted(object sender, EventArgs e) { Storyboard item = ... // snip ICommand command = GetCompletedCommand(item); command.Execute(null); } } </code></pre> <p>then I try to use it in XAML, with a Binding syntax:</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;Storyboard x:Key="myStoryboard" my:StoryboardExtensions.CompletedCommand="{Binding AnimationCompleted}"&gt; &lt;DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:5" /&gt; &lt;/Storyboard&gt; &lt;Style x:Key="myStyle" TargetType="{x:Type Label}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=QuestionState}" Value="Correct"&gt; &lt;DataTrigger.EnterActions&gt; &lt;BeginStoryboard Storyboard="{StaticResource myStoryboard}" /&gt; &lt;/DataTrigger.EnterActions&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;Label x:Name="labelHello" Grid.Row="0" Style="{StaticResource myStyle}"&gt;Hello&lt;/Label&gt; &lt;/Grid&gt; </code></pre> <p>This fails with the following exception:</p> <blockquote> <p>System.Windows.Markup.XamlParseException occurred Message="Cannot convert the value in attribute 'Style' to object of type 'System.Windows.Style'. Cannot freeze this Storyboard timeline tree for use across threads. Error at object 'labelHello' in markup file 'TestWpfApp;component/window1.xaml' </p> </blockquote> <p>Is there any way to get the Binding syntax working with an attached ICommand property for a Storyboard?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload