WPF MVVM模式开发简明实现教程 5 使用MultiValueConverter进行多参数事件绑定

一个事件有多个参数时,前面的方法就不能用了

虽然多个参数情况不多,还可以避免,但有必要知道下怎么做

 

View增加个button,只为了做参数传递

<Button Content="Binding 2" Height="40" VerticalAlignment="Bottom" x:Name="button2"></Button>

Model目录下增加ButtonModel.cs类

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

资源文件也区分开来,方便管理

<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下面

<UserControl.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="..\Style\MultiValueConverterResource.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

  

View事件改为

<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>

  多个参数就增加多个

<Binding ElementName="button1" />


ViewModel代码,可以看到param是前面你自定义的ButtonModel

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;
            }
        }

  运行效果

WPF MVVM模式开发简明实现教程 5 使用MultiValueConverter进行多参数事件绑定


上一篇:MVC和MVVM模型


下一篇:ABP领域层创建实体