WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior
1.Install Microsoft.Xaml.Behaviors.Wpf
2.
<behavior:Interaction.Triggers> <behavior:EventTrigger EventName="MouseDown"> <behavior:InvokeCommandAction Command="{Binding ClickCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/> </behavior:EventTrigger> </behavior:Interaction.Triggers> //cs private void InitCommands() { ClickCommand = new DelCmd(ClickCommandExecuted); } private void ClickCommandExecuted(object obj) { var win = obj as Window; if (win != null) { var pt = Mouse.GetPosition(win); MessageBox.Show($"{pt.X},{pt.Y}", "Clicked Position!"); } } public DelCmd ClickCommand { get; set; }
xaml
//xaml <Window x:Class="WpfApp1.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:WpfApp1" xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <behavior:Interaction.Triggers> <behavior:EventTrigger EventName="MouseDown"> <behavior:InvokeCommandAction Command="{Binding ClickCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/> </behavior:EventTrigger> </behavior:Interaction.Triggers> <Window.Resources> <SolidColorBrush Color="Cyan" x:Key="imgBg"/> </Window.Resources> <Window.DataContext> <local:BookVM/> </Window.DataContext> <Grid> <Image Source="/Images/1.jpg" RenderOptions.BitmapScalingMode="Fant"/> </Grid> </Window> //cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } public class BookVM { public BookVM() { InitCommands(); } private void InitCommands() { ClickCommand = new DelCmd(ClickCommandExecuted); } private void ClickCommandExecuted(object obj) { var win = obj as Window; if (win != null) { var pt = Mouse.GetPosition(win); MessageBox.Show($"{pt.X},{pt.Y}", "Clicked Position!"); } } public DelCmd ClickCommand { get; set; } } public class DelCmd : ICommand { public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } private Action<object> execute; private Predicate<object> canExecute; public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue) { execute = executeValue; canExecute = canExecuteValue; } public DelCmd(Action<object> executeValue) : this(executeValue, null) { } public bool CanExecute(object parameter) { if (canExecute == null) { return true; } return canExecute(parameter); } public void Execute(object parameter) { execute(parameter); } } }