0

I need to bind my storyboard's Completed to my VM DelegateCommand, so I found this topic.

But for some reason it doesn't work for me. Help me please figure it out.

My template with storyboard

<Grid.Triggers>
    <EventTrigger RoutedEvent="Button.Click" SourceName="AddButton2">
        <BeginStoryboard Name="MyBeginStoryboard">
            <Storyboard 
                TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" 
                local1:StoryboardHelper.CompletedCommand="{Binding Path=OpenPanelCommand}"
                >
                <DoubleAnimation 
                    Storyboard.TargetName="panelControl" 
                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
                    Duration="0:0:01" From="0" To="-700" 
                    />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Grid.Triggers>

ViewModel constructor, where OpenPanelCommand has a type ICommand:

OpenPanelCommand = new DelegateCommand(() => { PanelOpened = true; });

StoryboardHelper from link above:

public static class StoryboardHelper
{
    public static readonly DependencyProperty CompletedCommandProperty = DependencyProperty.RegisterAttached("CompletedCommand", typeof(ICommand), typeof(StoryboardHelper), new PropertyMetadata(null, OnCompletedCommandChanged));
    public static readonly DependencyProperty CompletedCommandParameterProperty = DependencyProperty.RegisterAttached("CompletedCommandParameter", typeof(object), typeof(StoryboardHelper), new PropertyMetadata(null));

    public static void SetCompletedCommand(DependencyObject o, ICommand value)
    {
        o.SetValue(CompletedCommandProperty, value);
    }

    public static ICommand GetCompletedCommand(DependencyObject o)
    {
        return (ICommand)o.GetValue(CompletedCommandProperty);
    }

    public static void SetCompletedCommandParameter(DependencyObject o, object value)
    {
        o.SetValue(CompletedCommandParameterProperty, value);
    }

    public static object GetCompletedCommandParameter(DependencyObject o)
    {
        return o.GetValue(CompletedCommandParameterProperty);
    }

    private static void OnCompletedCommandChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var sb = sender as Storyboard;

        if (sb != null)
        {
            sb.Completed += (a, b) =>
            {
                var command = GetCompletedCommand(sb);

                if (command != null)
                {
                    if (command.CanExecute(GetCompletedCommandParameter(sb)))
                    {
                        command.Execute(GetCompletedCommandParameter(sb));
                    }
                }
            };
        }
    }
}
Ioanna Ferens
  • 61
  • 1
  • 10
  • 1
    The helper is 100% working (just tested). Define please *"it doesn't work for me"*? Check "Output" window for binding errors and try some debugging: set breakpoint on the first line inside `OnCompletedCommandChanged`, does it gets hit? Do same with command lambda, etc. – Sinatr Nov 27 '17 at 13:34
  • yes you are right! the problem was with binding, thank you so much with giving me the right direction. – Ioanna Ferens Nov 27 '17 at 14:19

0 Answers0