Great for MVVM: Behavior that executes a Command on Double Click!

Did you ever wanted to accept a dialog when the user double clicks an item in a ListBox?

Now you can! This is the Interaction Behavior you have been looking for! apply it to any Selector derived class and it’s done! You only have to set the Command property of the behavior to any Command and it will execute when it user clicks an item twice. Woohoo!

I think it works like a charm.

public class SelectorDoubleClickBehavior : Behavior
    {
        private FrameworkElement element;

        protected override void OnAttached()
        {
            AssociatedObject.SelectionChanged += AssociatedObjectOnSelectionChanged;
            base.OnAttached();
        }

        private void AssociatedObjectOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            DettachEventIfAny();

            var selectedItem = AssociatedObject.SelectedItem;

            if (AssociatedObject.IsItemItsOwnContainer(selectedItem))
            {
                element = (FrameworkElement) selectedItem;
            }
            else
            {
                AssociatedObject.ItemContainerGenerator.ContainerFromItem(selectedItem);
            }

            element.PreviewMouseDown += ElementOnPreviewMouseDown;
        }

        protected override void OnDetaching()
        {
            DettachEventIfAny();

            base.OnDetaching();
        }

        private void DettachEventIfAny()
        {
            if (element != null)
            {
                element.PreviewMouseDown -= ElementOnPreviewMouseDown;
            }
        }

        private void ElementOnPreviewMouseDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            if (mouseButtonEventArgs.ClickCount == 2)
            {
                if (Command != null)
                {
                    Command.Execute(null);
                }
            }
        }

        #region Command
        public static readonly DependencyProperty CommandProperty =
          DependencyProperty.Register("Command", typeof(ICommand), typeof(SelectorDoubleClickBehavior),
            new FrameworkPropertyMetadata((ICommand)null));

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        #endregion

    }

Example of usage:

 
            Item 1
            Item 2


        

This is GOOD, boy!

EDIT: Do you know the best of this? The Cinch MVVM Library by Sacha Barber already included something very similar! But it behaves a diffently: if the command you associate to the behavior can not be executed, the Selector you applied the behavior to will be disabled!

Good job,  Sacha 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s