当前位置: 首页 > news >正文

wpf之Interaction.Triggers

前言

在wpf中 Interaction.Triggers是对命令的补充,命令能应用的场合有限,比如Button的单击事件,但是像window窗体的Loaded事件,TextBox的TextChanged就无法使用命令。Interaction.Triggers的结构如下:

<控件><i:Interaction.Triggers><触发器类型><动作类型 属性="值"/></触发器类型></i:Interaction.Triggers>
</控件>

1、引用dll

在NuGet包管理器中选择Microsoft.Xaml.Behaviors.Wpf这个包安装
在这里插入图片描述

2、引用命名空间

在xaml代码中加上如下代码

 xmlns:Event="http://schemas.microsoft.com/xaml/behaviors"

3、window的Loaded事件

下面的代码中EventName指定了需要绑定的事件的名称,这里是”Loaded“事件。在InvokeCommandAction的Command属性中绑定事件CommandWindowLoaded,这里的CommandWindowLoaded在ViewModel类中定义和实例化。

<Window x:Class="wpf之Interaction.Triggers.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:wpf之Interaction.Triggers"xmlns:Event="http://schemas.microsoft.com/xaml/behaviors"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Event:Interaction.Triggers><Event:EventTrigger EventName="Loaded"><Event:InvokeCommandAction Command="{Binding CommandWindowLoaded}"/></Event:EventTrigger></Event:Interaction.Triggers><Grid></Grid>
</Window>

自定义命令DelegateCommand

public class DelegateCommand : ICommand{private readonly Action<object> _execute;private readonly Func<object, bool> _canExecute;public DelegateCommand(Action<object> execute, Func<object, bool> canExecute = null){_execute = execute ?? throw new ArgumentNullException(nameof(execute));_canExecute = canExecute;}public bool CanExecute(object parameter){if (_canExecute != null){return _canExecute(parameter);}else{return true;}}public void Execute(object parameter){_execute(parameter);}public event EventHandler CanExecuteChanged{add => CommandManager.RequerySuggested += value;remove => CommandManager.RequerySuggested -= value;}}

定义ViewMode的基类ViewModelBase

 public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(string propname){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propname));}}}

定义ViewMode并且继承ViewModelBase,在ViewMode中实例化一个命令,

 public class ViewModel : ViewModelBase{public ICommand CommandWindowLoaded { get; set; }public ViewModel(){CommandWindowLoaded = new DelegateCommand(WindowLoaded);}public void WindowLoaded(object param){Console.WriteLine("窗体加载");}}

4、TextBox的TextChanged事件

当你改变TextBox内容的时候,不停的就会触发TextChangedCommand命令执行。
在这里插入图片描述

下面的代码同样是通过EventName指定需要绑定的事件类型,通过Command属性来绑定ViewModel中具体的事件,同时还可以传递参数。

<Window x:Class="wpf之Interaction.Triggers.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:wpf之Interaction.Triggers"xmlns:Event="http://schemas.microsoft.com/xaml/behaviors"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Event:Interaction.Triggers><Event:EventTrigger EventName="Loaded"><Event:InvokeCommandAction Command="{Binding CommandWindowLoaded}"/></Event:EventTrigger></Event:Interaction.Triggers><Grid><StackPanel ><TextBox x:Name="tbx_test" Height="20" Width=" 40"><Event:Interaction.Triggers><Event:EventTrigger EventName="TextChanged"><Event:InvokeCommandAction Command="{Binding TextChangedCommand}" CommandParameter="{Binding Path=Text,ElementName=tbx_test}"/></Event:EventTrigger></Event:Interaction.Triggers></TextBox ></StackPanel ></Grid>
</Window>
 public class ViewModel : ViewModelBase{public ICommand CommandWindowLoaded { get; set; }public ICommand TextChangedCommand { get; set; }public ViewModel(){CommandWindowLoaded = new DelegateCommand(WindowLoaded);TextChangedCommand= new DelegateCommand(TextChange);}public void WindowLoaded(object param){Console.WriteLine("窗体加载");}public void TextChange(object param){Console.WriteLine($"文本修改{param}");}}

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

http://www.dtcms.com/a/478949.html

相关文章:

  • 网站建设新的技术方案企业网站建设东莞
  • SSM管理系统c4ki9(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 珠海正规网站制作排名费用多少无锡阿凡达建设
  • asp网站建设 aws西安高校定制网站建设公司推荐
  • PyTorch 实现 MNIST 手写数字识别完整流程(含数据处理、模型构建与训练可视化)
  • 【Java并发编程】概念与核心问题、线程核心、并发控制、线程池、并发容器、并发问题
  • C++模板编程实战记录:SFINAE核心技术深度掌握
  • Spring Boot项目的常用依赖有哪些?
  • 保姆级教程 | ASE学习过程记录分析
  • 网站如何留言免费网站seo排名优化
  • 运维视角:SpringBootWeb框架全解析
  • Java Redis “运维”面试清单(含超通俗生活案例与深度理解)
  • 【组队学习】Post-training-of-LLMs TASK01
  • 涉县网站网络推广培训哪里好
  • Jenkins自动化配置--CICD流水线
  • 网站建设etw深圳租赁住房和建设局网站
  • 人力网站建设的建议wordpress加百度广告代码出问题
  • Mozilla 项目
  • 今日行情明日机会——20251013
  • 关于解决js中MediaRecorder录制的webm视频没有进度条的问题
  • 红日靶场(二)学习过程详细记录
  • 【多线程】门栓/闭锁(Latch/CountDownLatch)
  • [1-02-02].[第01章:HTML + CSS
  • 手机必备网站软件技术专科生的出路
  • 网站空间续费一年多少钱怎么弄推广广告
  • 一个做任务的网站如何绑定域名wordpress
  • 当ubuntu 系统的IP地址修改之后,gitlab服务应该如何修改?
  • 怎么做自己的公司网站本地服务器 wordpress
  • 网站制作 优帮云做淘宝客网站需要做后台吗
  • xsync.sh分发脚本和命令执行脚本