C# wpf 转换器、向事件传递参数

以下两个案例实现一个效果

20241106_145054

一、有Command属性控件绑定事件

1.xaml中创建视图

 <TextBox x:Name="a" />
 <TextBox x:Name="b" />
 <TextBox x:Name="c" />
 <Button
     Width="100"
     Height="30"
     Command="{Binding Command3}"
     Content="传递多个参数">
     <Button.CommandParameter>
         <MultiBinding Converter="{StaticResource objectToArryConverter}">
             <Binding ElementName="a" Path="Text" />
             <Binding ElementName="b" Path="Text" />
             <Binding ElementName="c" Path="Text" />
         </MultiBinding>
     </Button.CommandParameter>
 </Button>

2.创建ViewModel视图模型

public class MainWindowViewModel : ObservableObject
{
    public RelayCommand<object[]> Command3
    {
        get
        {
            return new RelayCommand<object[]>((arr) =>
            {
				MessageBox.Show(arr[0].ToString());
                MessageBox.Show(arr[1].ToString());
                MessageBox.Show(arr[2].ToString());
            });
        }
    }
}

3.在xaml中引用视图模型 

<Window
    xmlns:vm="clr-namespace:wpf转换器.ViewModel"
    d:DataContext="{d:DesignInstance Type=vm:MainWindowViewModel}">

 <Window.DataContext>
     <vm:MainWindowViewModel />
 </Window.DataContext>

 4.创建一个 Converter文件夹存放转换器,并在其中创建一个object类型转为数组类型的转换器

 public class ObjectToArryConverter : IMultiValueConverter
 {
     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
     {
         return  values.ToArray();
     }

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

5.在xaml中引用转换器 

<Window
   xmlns:i="http://schemas.microsoft.com/xaml/behaviors">

 <Window.Resources>
     <c:ObjectToArryConverter x:Key="objectToArryConverter" />
 </Window.Resources>

二、没有Command属性控件绑定事件 

1.安装:程序包

2.创建转换器

 public class ObjectToArryConverter : IMultiValueConverter
 {
     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
     {
         return  values.ToArray();
     }

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

3.xaml中引入转换器:

<Window
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors">

 <Window.Resources>
     <c:ObjectToArryConverter x:Key="objectToArryConverter" />
 </Window.Resources>

4.xaml中创建视图

<TextBox x:Name="a" />
<TextBox x:Name="b" />
<TextBox x:Name="c" />
<TextBlock
    Width="100"
    Height="30"
    Text="传递多个参数">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <i:InvokeCommandAction Command="{Binding Command3}">
                <i:InvokeCommandAction.CommandParameter>
                    <MultiBinding Converter="{StaticResource objectToArryConverter}">
                        <Binding ElementName="a" Path="Text" />
                        <Binding ElementName="b" Path="Text" />
                        <Binding ElementName="c" Path="Text" />
                    </MultiBinding>
                </i:InvokeCommandAction.CommandParameter>
            </i:InvokeCommandAction>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBlock>

 5.创建ViewModel视图模型

public class MainWindowViewModel : ObservableObject
{
    public RelayCommand<object[]> Command3
    {
        get
        {
            return new RelayCommand<object[]>((arr) =>
            {
				MessageBox.Show(arr[0].ToString());
                MessageBox.Show(arr[1].ToString());
                MessageBox.Show(arr[2].ToString());
            });
        }
    }
}

6.在xaml中引用视图模型 

<Window
    xmlns:vm="clr-namespace:wpf转换器.ViewModel"
    d:DataContext="{d:DesignInstance Type=vm:MainWindowViewModel}">

 <Window.DataContext>
     <vm:MainWindowViewModel />
 </Window.DataContext>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值