【WPF命令绑定之--没有Command属性的控件如何进行命令绑定?】

news/2025/3/1 18:38:33/

前言

C#WPF之命令绑定

内容

有些控件不支持直接绑定命令,可以调用其他依赖实现命令的绑定。

依赖:Microsoft.Xaml.Behaviors.Wpf

使用如下代码可以实现事件的命令绑定,及传递参数:

1、引用:xmlns:behavior=“http://schemas.microsoft.com/xaml/behaviors”
2、添加命令绑定代码( 如:代码①)
3、创建实现ICommand 的类public class EventsCommand : ICommand(如代码②)
4、创建命令 public ICommand LoadedWindowCommand{ get; set; }
5、初始化命令、并创建方法(如:代码③)

其中代码①写在**.xaml窗体视图中。代码②单独创建类。命令的初始化及方法绑定是创建在视图对应的ViewModel中如代码③。

代码①
<!--绑定上下文-->
<Window.DataContext><viewmodels:MainViewModel/>
</Window.DataContext><!--事件命令绑定-->
<behavior:Interaction.Triggers><!--窗体加载命令绑定--><behavior:EventTrigger EventName="Loaded"><behavior:InvokeCommandAction Command="{Binding LoadedWindowCommand}"   PassEventArgsToCommand="True"/></behavior:EventTrigger><!--窗体关闭命令绑定--><behavior:EventTrigger EventName="Closing"><behavior:InvokeCommandAction Command="{Binding ClosingWindowCommand}" PassEventArgsToCommand="True"/></behavior:EventTrigger>
</behavior:Interaction.Triggers>
代码②
public class EventsCommand<T> : ICommand
{private readonly Action<T> _execute;private readonly Func<T, bool> _canExecute;public EventsCommand(Action<T> execute, Func<T, bool> canExecute = null){_execute = execute ?? throw new ArgumentNullException(nameof(execute));_canExecute = canExecute;}public bool CanExecute(object parameter){return _canExecute?.Invoke((T)parameter) ?? true;}public void Execute(object parameter){_execute((T)parameter);}public event EventHandler CanExecuteChanged{add { CommandManager.RequerySuggested += value; }remove { CommandManager.RequerySuggested -= value; }}
}
代码③
public class MainViewModel:INotifyPropertyChanged
{public ICommand LoadedWindowCommand { get; set; }public ICommand ClosingWindowCommand { get; set; }public MainViewModel(){LoadedWindowCommand = new EventsCommand<object>(OnLoadedWindow);ClosingWindowCommand = new EventsCommand<object>(OnClosingWindow);}private void OnLoadedWindow(object e){}private void OnClosingWindow(object e){}
}

命令绑定例外

描述

有一种情况是控件里面添加控件,次数使用命令绑定传递参数。使用事件时,传递的一般是外层控件对象。

那么如何实现传递里面的对象呢,可以使用如下方法:使用DataContext数据上下文来传递。

<ScrollViewer Background="#AEAEAE" x:Name="RecordScrollViewer"><ListBox ItemsSource="{Binding ChatRecordCollection}" Margin="5"><ListBox.ItemTemplate><DataTemplate><!-- 显示消息内容 --><TextBlock Text="{Binding Data}"  Margin="10,0,0,0"><behavior:Interaction.Triggers><!--鼠标点击命令事件--><behavior:EventTrigger EventName="PreviewMouseDown"><behavior:InvokeCommandActionCommand="{Binding DataContext.ChatRecordMouseDownCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"CommandParameter="{Binding}"PassEventArgsToCommand="True"/></behavior:EventTrigger></behavior:Interaction.Triggers></TextBlock></DataTemplate></ListBox.ItemTemplate></ListBox>
</ScrollViewer>

结语

以上是个人日常学习中学到的一下知识,和理解。


http://www.ppmy.cn/news/1575838.html

相关文章

【Docker】使用Docker搭建-MySQL数据库服务

零、更换Docker镜像源 因为国内现在封锁了Docker默认拉取镜像的站点&#xff08;DockerHub&#xff09;&#xff0c;而且国内大部分Docker镜像站已全部下线&#xff0c;导致现在很多朋友在拉取镜像的时候会出现无法拉取的现象&#xff0c;这时候就需要进行更换Docker镜像源。 可…

加油站小程序实战教程04类目级联选择

目录 1 完善油号、油枪新增功能2 配置级联选择总结 后台我们目前增加了类目和站点信息&#xff0c;当录入站点信息时候&#xff0c;因为有一级类目和二级类目&#xff0c;我们需要做级联选择&#xff0c;本篇我们完善一下油号、油枪的新增功能&#xff0c;实现一下站点类目信息…

网络安全导论PDF

&#x1f345; 点击文末小卡片 &#xff0c;免费获取网络安全全套资料&#xff0c;资料在手&#xff0c;涨薪更快 这份重点是在准备复试时边看书和ppt边手打的。掐指一算已经是整整一个月前的事情惹。 这本教材是哈工程复试参考书目&#xff0c;但是网络上关于它的材料比较少。…

Spring Cloud Gateway 网关的使用

在之前的学习中&#xff0c;所有的微服务接口都是对外开放的&#xff0c;这就意味着用户可以直接访问&#xff0c;为了保证对外服务的安全性&#xff0c;服务端实现的微服务接口都带有一定的权限校验机制&#xff0c;但是由于使用了微服务&#xff0c;就需要每一个服务都进行一…

会话与会话管理:Cookie与Session的深度解析

一、什么是会话&#xff1f; 二、Cookie&#xff1a;客户端存储技术 1. Cookie的工作原理 2、在后端设置cookie 3、在前端设置cookie 三、浏览器开启了cookie禁用怎么办&#xff1f; 一、什么是会话&#xff1f; 会话&#xff08;Session&#xff09;是指一个用户与服务器之间…

CES Asia 2025前瞻:网络安全与数据隐私成焦点

在数字化转型的浪潮加速奔涌之际&#xff0c;网络安全与数据隐私已然成为企业发展版图中的核心关切。即将盛大启幕的CES Asia 2025第七届亚洲消费电子技术贸易展&#xff08;赛逸展&#xff09;&#xff0c;无疑将汇聚行业目光&#xff0c;成为探讨网络安全与数据隐私领域前沿趋…

PySpark中mapPartitionsWithIndex等map类算子生成器函数问题 - return\yield

PySpark中mapPartitionsWithIndex等map类算子生成器函数问题 - return\yield 顾名思义&#xff0c;本文讲述了map算子生成器函数的相关问题——return 和 yield的使用。 首先先讲结论&#xff0c;在使用map等迭代生成的算子时最好使用yield。 1、问题产生 在写代码的过程中&…

github上传代码(自用)

github上传代码&#xff08;自用&#xff09; 前提&#xff1a;git已与github完成秘钥连接配置 一、克隆仓库法&#xff1a; 把远程仓库克隆到本地&#xff0c;会自动生成仓库配置&#xff0c;不用自己连接 1、github建立远程仓库 2、新手引导&#xff08;创建仓库时不要创…