首页 > 其他分享 >WPF事件转命令

WPF事件转命令

时间:2022-11-02 08:57:08浏览次数:51  
标签:ImageMouseUp 命令 添加 事件 str WPF public

<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

相关文章

  • CMD命令设置IP
    “以太网”根据网络连接里的名子进行修改,注意红字一个是set一个是add,下面两行是增加双IPnetshinterfaceipv4setaddress"以太网"static10.10.11.116255.255.25......
  • Javascript进阶笔记 - 事件
    事件目录事件1.事件相关概念2.文档的加载3.事件的冒泡4.事件的委派5.事件监听绑定1.事件相关概念事件是电脑输入设备与页面进行交互的响应。注册就是告诉游......
  • Javascript进阶笔记 - 常用事件
    常用事件目录常用事件1.onload事件2.onclick事件3.onblur事件4.onchange事件5.onsubmit事件6.onscroll事件7.onmousemove事件8.onmousedown&onmouseup事件9.on......
  • Linux命令
    #linus/终端的常用快捷键【ll】显示当前目录的所有文件【详】【ls】显示当前目录的所有文件【略】【ls/路径】显示该路径下的所有文件【cd..】进入上级目录【./】......
  • 有可能是学习Git命令最好的网站
    网站地址:https://learngitbranching.js.org/?locale=zh_CN网站以沙盒闯关的方式学习Git各个命令,每次关卡都介绍本次要学习的Git指令,通过沙盒+命令行的方式来引导你通过已......
  • linux系统基础命令
    ls命令:/:根目录,linux系统只有一个跟目录ls-a:显示出隐藏的内容,以.开头的文件默认默认被隐藏,需要-a才能显示出来ls-l以列表的形式将内容显示出来ls-h可以与-l搭配使用,......
  • Linux学习笔记(第五篇)归档及压缩命令
    ​​Linux学习笔记(第零篇)计算机基础​​Linux学习笔记(第零篇)Linux文件系统及ShellLinux学习笔记(第一篇)零散命令Linux学习笔记(第二篇)目录操作命令Linux学习笔记(第三篇)文件操......
  • PowerPlatform 通过命令栏添加自定义按钮
    在这之前,要添加自定义按钮都是通过RibbonWorkbench,现在可以直接在ModelDriven编辑界面通过命令栏编辑添加自定义按钮  在需要添加按钮的页面,选择编辑命令栏,并选择......
  • Linux学习笔记(第一篇)零散命令
    ​​Linux学习笔记(第零篇)计算机基础​​Linux学习笔记(第零篇)Linux文件系统及ShellLinux学习笔记(第一篇)零散命令Linux学习笔记(第二篇)目录操作命令Linux学习笔记(第三篇)文件操......
  • Linux学习笔记(第二篇)目录操作命令
    ​​Linux学习笔记(第零篇)计算机基础​​Linux学习笔记(第零篇)Linux文件系统及ShellLinux学习笔记(第一篇)零散命令Linux学习笔记(第二篇)目录操作命令Linux学习笔记(第三篇)文件操......