Here is an example of using RightMouseClickEvent in MVVM. You could add system.windows.interactivity.dll
to the project.
MainWindow.xaml:
<Window x:Class="RightMouseClickEventOfRectangleMVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RightMouseClickEventOfRectangleMVVM"
mc:Ignorable="d"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<Rectangle x:Name="rect" Fill="{Binding MyColor}" HorizontalAlignment="Left"
Height="100" Margin="10,29,0,0" Stroke="Black" VerticalAlignment="Top"
Width="100" MouseRightButtonDown="rect_MouseRightButtonDown">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseRightButtonDown">
<i:InvokeCommandAction Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
<Button x:Name="btn" Command="{Binding Button1Command}" Content="GO"/>
</StackPanel>
</Window>
MainWindow.xaml.cs:
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace RightMouseClickEventOfRectangleMVVM
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void rect_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("clicked");
}
}
public class ViewModel : INotifyPropertyChanged
{
private Brush myColor = Brushes.Red;
public Brush MyColor
{
get
{
return myColor;
}
set
{
myColor = value;
NotifyPropertyChanged("MyColor");
}
}
public DelegateCommand MyCommand { get; set; }
public DelegateCommand Button1Command { get; set; }
public ViewModel()
{
Button1Command = new DelegateCommand(onButton1Command);
MyCommand = new DelegateCommand(OnRectangleClicked);
}
private void OnRectangleClicked(object obj)
{
MyColor = Brushes.Pink;
}
private void onButton1Command(object obj)
{
MessageBox.Show("hello");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(this, args);
}
}
}
public class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html