Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

c# - Passing two command parameters on button click on WPF NET 3.5 MVVM

I am trying to pass two ICommand parameters on button click to a method in view model.

Now I am able to pass only one parameter.

Code here.

XAML (View):

<Button x:Name="btnAdd" Command="{Binding AddUserCommand}" 
                        CommandParameter="{Binding IDUser}"/>

View Model:

    public string IDUser
    {
        get
        {
            return this.personalData.UserID;
        }

        set
        {
            if (this.personalData.UserID == value)
            {
                return;
            }

            this.personalData.UserID = value;
            OnPropertyChanged("UserID");
        }
    }


    private RelayCommand addUserCommand;
    public ICommand AddUserCommand
    {
        get
        {
            return addUserCommand ??
                (addUserCommand = new RelayCommand(param => this.AddUser(param.ToString())));
        }
    }

    public vol AddUser(string userId)
    {
         // Do some stuff
    }

Now I want to pass another ICommand parameter on button click. The parameter I want to pass is the value (checked or not) from a checkbox.

<CheckBox x:Name="Status" Content="Married"/>

so that method AddUser in view model will have below signature:

    public vol AddUser(string userId, bool status)
    {
         // Do some stuff
    }

I know it can be done using MultiBinding in combination with a converter but I do not know exactly how to do it. Also I am not understanding at all why a converter is necessary when using multibinding.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A command takes only one parameter. The Execute method of the ICommand interface is defined as:

void Execute(object parameter);

So you can't pass two parameters to a command.

What you could do is to pass an instance of a class that has two properties. This is where the multi converter comes in.

Bind the CommandParameter to two properties and use the converter to return one object.

XAML:

<Button x:Name="btnAdd" Command="{Binding AddUserCommand}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource yourConverter}">
            <Binding Path="IDUser" />
            <Binding Path="IsChecked" ElementName="Status" />
        </MultiBinding>
    </Button.CommandParameter>
</Button>

Converter:

public class Converter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return new YourClass() { IdUser = values[0] as string, IsChecked = System.Convert.ToBoolean(values[1]) };
    }

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

View Model:

private RelayCommand<YourClass> addUserCommand;
public ICommand AddUserCommand
{
    get
    {
        return addUserCommand ??
            (addUserCommand = new RelayCommand<YourClass>(param => this.AddUser(param)));
    }
}

public vol AddUser(YourClass obj)
{
    string IDUser = obj.IDUser;
    bool isChecked = obj.IsChecked;
    // Do some stuff
}

Of course you must also define the YourClass payload type with the two parameters IDUser and IsChecked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...