Created
September 20, 2013 14:08
-
-
Save Koki-Shimizu/6638119 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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