<Window x:Class="WpfAppDemo.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:WpfAppDemo" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:ViewModel="clr-namespace:WpfAppDemo.ViewModel" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.DataContext> <ViewModel:MainViewModel/> </Window.DataContext> <Grid> <Image Source="https://www.toopic.cn/public/uploads/small/1642751451645164275145189.jpg"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseUp"> <i:InvokeCommandAction Command="{Binding MouseUpCommand}" CommandParameter="ImageMouseUp"/> </i:EventTrigger> </i:Interaction.Triggers> </Image> </Grid> </Window>
ViewModel:
public class MainViewModel { public ICommand MouseUpCommand { get => new RelayCommand<string>(ImageMouseUp); } private void ImageMouseUp(string str) { MessageBox.Show(str); } }
- 下载程序安装包 Microsoft.Xaml.Behaviors.Wpf
- 添加引用:xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
- 在需要转命令的控件添加:
<i:Interaction.Triggers> <i:EventTrigger EventName="MouseUp【需要的事件】"> <i:InvokeCommandAction Command="{Binding 【命令】}" CommandParameter="传递的参数"/> </i:EventTrigger> </i:Interaction.Triggers>
- 效果:
标签:ImageMouseUp,命令,添加,事件,str,WPF,public From: https://www.cnblogs.com/lxiamul/p/16848504.html