一、概述

有时候,单纯的命令绑定不一定能满足我们的开发需求,比如我们需要在命令绑定的时候传递一个参数,这个时候,我们就需要使用RelayCommand的泛型版本了。

RelayCommand的泛型版本的构造函数以下:

public RelayCommand(Action<T> execute, bool keepTargetAlive = false);
public RelayCommand(Action<T> execute, Func<T, bool> canExecute, bool keepTargetAlive = false);

构造函数传入的是委托类型的参数,Execute 和 CanExecute执行委托方法。

二、带一个参数的命令绑定

代码片段如下:

MVVMLight学习笔记(五)---RelayCommand深究
<StackPanel>
            <GroupBox Header="带string类型参数的命令" BorderBrush="#FF11519C" BorderThickness="1" FontSize="16" Foreground="#FFCDAA0C" Margin="2">
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="UserList:" VerticalContentAlignment="Center" FontSize="20" ></Label>
                        <Label Content="{Binding Path=UserList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" FontSize="20" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="UserName:" VerticalContentAlignment="Center" FontSize="20" ></Label>
                        <TextBox Width="200" Name="tbUser"></TextBox>
                        <Button Content="AddUser" Command="{Binding AddUserCommand}" CommandParameter="{Binding ElementName=tbUser,Path=Text}"></Button>
                        <CheckBox Content="IsCanAdd" VerticalAlignment="Center" FontSize="16" IsChecked="{Binding IsCanAddUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
                    </StackPanel>
                </StackPanel>
            </GroupBox>
        </StackPanel>
MVVMLight学习笔记(五)---RelayCommand深究
MVVMLight学习笔记(五)---RelayCommand深究
 private RelayCommand<string> addUserCommand;

        public RelayCommand<string> AddUserCommand
        {
            get
            {
                if (addUserCommand == null)
                {
                    addUserCommand = new RelayCommand<string>(AddUser, (string p) => { return IsCanAddUser; });
                }
                return addUserCommand;
            }
            set { addUserCommand = value; }
        }
        private void AddUser(string par)
        {
            UserList = UserList + "  " + par;
        }
MVVMLight学习笔记(五)---RelayCommand深究

三、带多个参数的命令绑定

给命令传递多个参数,建议使用以下方式:

使用MultiBinding将多绑定的各个值转换成我们所需的对象或者实例模型,再传递给ViewModel中的命令。

代码片段如下:

MVVMLight学习笔记(五)---RelayCommand深究
<Window x:Class="MvvmLightDemo1.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:MvvmLightDemo1"
        xmlns:cvt="clr-namespace:MvvmLightDemo1.Converter"
        xmlns:mvvm="http://www.galasoft.ch/mvvmlight"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        Title="MVVMLIghtDemo1" Height="500" Width="700"  >
    <Window.Resources>
        <cvt:UserInfoConverter x:Key="userInfoConverter"></cvt:UserInfoConverter>
    </Window.Resources>
    <Window.DataContext>
        <Binding Path="Main" Source="{StaticResource Locator}"></Binding>
    </Window.DataContext>
    <StackPanel>
        <StackPanel>
            <GroupBox Header="带string类型参数的命令" BorderBrush="#FF11519C" BorderThickness="1" FontSize="16" Foreground="#FFCDAA0C" Margin="2">
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="UserList:" VerticalContentAlignment="Center" FontSize="20" ></Label>
                        <Label Content="{Binding Path=UserList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" FontSize="20" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="UserName:" VerticalContentAlignment="Center" FontSize="20" ></Label>
                        <TextBox Width="200" Name="tbUser"></TextBox>
                        <Button Content="AddUser" Command="{Binding AddUserCommand}" CommandParameter="{Binding ElementName=tbUser,Path=Text}"></Button>
                        <CheckBox Content="IsCanAdd" VerticalAlignment="Center" FontSize="16" IsChecked="{Binding IsCanAddUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></CheckBox>
                    </StackPanel>
                </StackPanel>
            </GroupBox>
        </StackPanel>

        <StackPanel>
            <GroupBox Header="带对象类型参数的命令" BorderBrush="#FF11519C" BorderThickness="1" FontSize="16" Foreground="#FF127C0D" Margin="2">
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="UserName:" FontSize="16" ></Label>
                        <TextBox Width="200" Name="tbxUser" FontSize="16" />
                        <Label Content="Password:" FontSize="16" ></Label>
                        <TextBox Width="200" Name="tbxPwd" FontSize="16" />
                        <Button Content="AddUser" Command="{Binding AddUserCommandWithObjPar}">
                            <Button.CommandParameter>
                                <MultiBinding Converter="{StaticResource userInfoConverter}">
                                    <Binding ElementName="tbxUser" Path="Text"/>
                                    <Binding ElementName="tbxPwd" Path="Text"/>
                                </MultiBinding>
                            </Button.CommandParameter>
                            
                        </Button>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="Parameter:"  FontSize="16" ></Label>
                        <Label Content="{Binding ObjParameter}"  FontSize="16" ></Label>

                    </StackPanel>
                </StackPanel>
            </GroupBox>
        </StackPanel>

        <StackPanel>
            <GroupBox Header="事件转命令" BorderBrush="#FF11519C" BorderThickness="1" FontSize="16" Foreground="#FFCDAA0C" Margin="2">
                <StackPanel>
                    <StackPanel>
                        <ListBox x:Name="lb" ItemsSource="{Binding ListBoxData}"  BorderThickness="0" SelectedIndex="{Binding SelectIndex}" >
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="SelectionChanged">
                                    <mvvm:EventToCommand Command="{Binding SelectionChangedCommand}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"/>
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>

                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Border BorderBrush="AntiqueWhite" BorderThickness="1">
                                        <StackPanel Margin="2">

                                            <Image Source="{Binding Img}" Width="96" Height="96"/>
                                            <TextBlock HorizontalAlignment="Center" Text="{Binding Info}"/>
                                
                                       
                                    </StackPanel>
                                    </Border>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                        <Label Content="您选择的是:" FontSize="16" ></Label>
                        <Label Content="{Binding Path=SelResult,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" FontSize="16" />
                    </StackPanel>
                </StackPanel>
            </GroupBox>
        </StackPanel>

    </StackPanel>
</Window>
MVVMLight学习笔记(五)---RelayCommand深究
MVVMLight学习笔记(五)---RelayCommand深究
MVVMLight学习笔记(五)---RelayCommand深究
 1 using GalaSoft.MvvmLight;
 2 
 3 namespace MvvmLightDemo1.ViewModel
 4 {
 5     public class UserModel: ObservableObject
 6     {
 7         private string userName;
 8 
 9         public string UserName
10         {
11             get { return userName; }
12             set
13             {
14                 userName = value;
15                 RaisePropertyChanged();
16             }
17         }
18 
19         private string passWord;
20 
21         public string PassWord
22         {
23             get { return passWord; }
24             set
25             {
26                 passWord = value;
27                 RaisePropertyChanged();
28             }
29         }
30 
31 
32     }
33 }
MVVMLight学习笔记(五)---RelayCommand深究

相关文章: