WPF MVVM模式开发简明实现教程 5 使用MultiValueConverter进行多参数事件绑定
WPF MVVM模式开发实现简明教程 2 初识 INotifyPropertyChanged
WPF MVVM模式开发实现简明教程 3-1 BaseCommand
WPF MVVM模式开发实现简明教程 4 ViewModelBase
WPF MVVM模式开发简明实现教程 5 使用MultiValueConverter进行多参数事件绑定
WPF MVVM模式开发简明实现教程 7 DevExpress MVVM
一个事件有多个参数时,前面的方法就不能用了
虽然多个参数情况不多,还可以避免,但有必要知道下怎么做
View增加个button,只为了做参数传递
<Button Content="Binding 2" Height="40" VerticalAlignment="Bottom" x:Name="button2"></Button>
Model目录下增加ButtonModel.cs类
1 2 3 4 5 6 7 8 9 10 11 | namespace WpfApp6.Model { using System.Windows.Controls; public class ButtonModel { public Button btn1 { get ; set ; } public Button btn2 { get ; set ; } } } |
这个model是你要传递的多个参数,多个就继续增加就行,这里只演示2个
Converter目录下增加个类ButtonMultiValueConverter.cs
namespace WpfApp6.Converter { using System; using System.Globalization; using System.Windows.Controls; using System.Windows.Data; using WpfApp6.Model; public class ButtonMultiValueConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { Button btn1 = values[0] as Button; Button btn2 = values[1] as Button; ButtonModel b = new ButtonModel(); b.btn1 = btn1; b.btn2 = btn2; return b; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }
这个实现了IMultiValueConverter接口,values包含你传的参数,还是多个就处理多个
Style目录下增加资源字典文件MultiValueConverterResource.xaml
资源文件也区分开来,方便管理
1 2 3 4 5 6 7 | <ResourceDictionary xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:multiValueConverter= "clr-namespace:WpfApp6.Converter" xmlns:local= "clr-namespace:WpfApp6.Style" > <multiValueConverter:ButtonMultiValueConverter x:Key= "buttonMultiValueConverter" /> </ResourceDictionary> |
View里引用刚刚加的资源字典,放在DataContext下面
1 2 3 4 5 6 7 | <UserControl.Resources> <ResourceDictionary > <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source= "..\Style\MultiValueConverterResource.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> |
View事件改为
1 2 3 4 5 6 7 8 9 10 11 12 | <b:Interaction.Triggers> <b:EventTrigger EventName= "Click" > <b:InvokeCommandAction Command= "{Binding Button_Click3}" > <b:InvokeCommandAction.CommandParameter > <MultiBinding Converter= "{StaticResource buttonMultiValueConverter}" > <Binding ElementName= "button1" /> <Binding ElementName= "button2" /> </MultiBinding> </b:InvokeCommandAction.CommandParameter> </b:InvokeCommandAction> </b:EventTrigger> </b:Interaction.Triggers> |
多个参数就增加多个
1 | <Binding ElementName= "button1" /><br><br><br> |
ViewModel代码,可以看到param是前面你自定义的ButtonModel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | private ICommand button_Click3; public ICommand Button_Click3 { get { if (button_Click3 == null ) { button_Click3 = new BaseCommand((param) => { ButtonModel buttons = param as ButtonModel; MessageBox.Show( "button:" + buttons.btn1.Content.ToString() + " button2:" + buttons.btn2.Content.ToString()); //.... }); } return button_Click3; } } |
运行效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架