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🙂

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s