Skip to content

Instantly share code, notes, and snippets.

@Bruskyer
Forked from Koki-Shimizu/MainWindow.xaml
Created January 20, 2020 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bruskyer/a987bd9a5ccc5727bc87b6f938289d22 to your computer and use it in GitHub Desktop.
Save Bruskyer/a987bd9a5ccc5727bc87b6f938289d22 to your computer and use it in GitHub Desktop.
Generally, WPF ContextMenu is opened right click on a control. Any user don't like right click. I try to open ContextMenu left clicking. DataContext is not binding using right click for some reason. This is simple solution for that problem.
<Window x:Class="ContextMenuBindingSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Grid>
<Button MouseRightButtonUp="Button_MouseRightButtonUp" PreviewMouseLeftButtonDown="Button_MouseLeftButtonDown" Content="StaticContext" HorizontalAlignment="Left" Margin="29,29,0,0" VerticalAlignment="Top" Width="125" Height="68">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding StartName}" Command="{Binding StartCommand}"/>
<MenuItem Header="{Binding EndName}" Command="{Binding EndCommand}"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel() { StartName = "start menu", EndName = "end menu" };
}
private void Button_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var button = sender as Button;
if (button != null)
{
// Why is button.ContextMenu.DataContext null? ... when right click is O.K. but leftdown is N.G.
button.ContextMenu.DataContext = button.DataContext;
button.ContextMenu.IsOpen = true;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace ContextMenuBindingSample
{
class MainWindowViewModel : INotifyPropertyChanged
{
private string _StartName;
public string StartName
{
get
{
return _StartName;
}
set
{
_StartName = value;
NotifyPropertyChanged("StartName");
}
}
public ICommand _StaftCommand;
public ICommand StartCommand
{
get
{
if (_StaftCommand == null)
{
_StaftCommand = new StartCommand();
}
return _StaftCommand;
}
}
public ICommand _EndCommand;
public ICommand EndCommand
{
get
{
if (_EndCommand == null)
{
_EndCommand = new EndCommand();
}
return _EndCommand;
}
}
private string _EndName;
public string EndName
{
get
{
return _EndName;
}
set
{
_EndName = value;
NotifyPropertyChanged("EndName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
class StartCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show("StartCommand");
}
}
class EndCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show("EndCommand");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment