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(
        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)

            if (logicalSource.DataContext != logicalSender.DataContext)

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

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


Sample implementation

<UserControl x:Class="Seekford.View"

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

Happy coding!


0 thoughts on “LeftClick Context Menu behavior in WPF or Silverlight”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.