LeftClick Context Menu behavior in WPF or Silverlight

Sometimes you just have to be different and want the context menu to appear when you left click as well.

Nothing is wrong with being different, so here is a behavior to do just than.

I assume you know what a behavior is and how to implement it, right? If not, your at the wrong place.

Note: I use a button for this, easier to capture a nice Click.

Behavior code class:

using System;
using System.Windows;
using System.Windows.Controls;

namespace Seekford.Behaviors{
    public static  class LeftClickContextBehavior
    {

        private static void OnHandleEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ele = d as Button;
            if (ele != null)
            {
                ele.Click -= OnClick;               
                if (e.NewValue != null && ((bool)e.NewValue))
                {
                    ele.Click += OnClick;                   
                }
            }
        }

        public static readonly DependencyProperty EnabledProperty
            = DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(LeftClickContextBehavior),
                                                  new FrameworkPropertyMetadata(false,
                                                                                new PropertyChangedCallback(
                                                                                    OnHandleEnabledChanged)));
        public static bool GetEnabled(DependencyObject dependencyObj)
        {
            if (dependencyObj == null)
                throw new ArgumentNullException("dependencyObj");

            return (bool)dependencyObj.GetValue(EnabledProperty);
        }

        public static void SetEnabled(DependencyObject dependencyObj, bool value)
        {
            if (dependencyObj == null)
                throw new ArgumentNullException("dependencyObj");

            dependencyObj.SetValue(EnabledProperty, value);
        }

        private static void OnClick(object sender, RoutedEventArgs  e)
        {
            var ele = sender as Button;
            var originalSender = e.OriginalSource as DependencyObject;
            var logicalSender = e.OriginalSource as FrameworkElement;
            var logicalSource = e.Source as FrameworkElement;

            if (ele == null || originalSender == null || sender == null || logicalSource == null || logicalSender == null)
                return;

            if (logicalSource.DataContext != logicalSender.DataContext)
                return;

            var dependencyObject = sender as DependencyObject;
            if (dependencyObject == null)
                return;

            if (ele.ContextMenu != null)
            {
                ele.ContextMenu.PlacementTarget = ele;
                ele.ContextMenu.IsOpen = true;
            }
        }

    }
}


Sample implementation

<UserControl x:Class="Seekford.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
             xmlns:Behaviors="clr-namespace:Seekford.Behaviors"
>


<Button Behaviors:LeftClickContextBehavior.Enabled="True">
     <Button.ContextMenu >
      <ContextMenu ItemsSource="{Binding MenuStuff}" >
           <ContextMenu.ItemTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Name}"/>
           </DataTemplate>
        </ContextMenu.ItemTemplate>
      </ContextMenu>
    </Button.ContextMenu>
    <Button.Template>
        <ControlTemplate>
          <StackPanel>
               <Textblock Text=”test”/>
         </StackPanel>
         </ControlTemplate>
    </Button.Template>
</Button >


Happy coding!

 

0 Replies to “LeftClick Context Menu behavior in WPF or Silverlight”

Leave a Reply

Your email address will not be published. Required fields are marked *