Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple command parameters wpf button object

How can I send multiple parameters from Button in WPF? I am able to send single parameter which is value of TextBox properly. Here is the code.

XAML

<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="133,22,0,0"     Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Grid.Row="1" Height="23" Command="{Binding Path=CommandClick}" CommandParameter="{Binding Text,ElementName=textBox1}" HorizontalAlignment="Left" Margin="133,62,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

Code behind

public ICommand CommandClick { get; set; }

this.CommandClick = new DelegateCommand<object>(AddAccount);

private void AddAccount(object obj)
{
    //custom logic
}
like image 722
Mandar Jogalekar Avatar asked Apr 11 '13 15:04

Mandar Jogalekar


People also ask

How do you pass a command parameter in WPF MVVM?

Passing a parameter to the CanExecute and Execute methods A parameter can be passed through the "CommandParameter" property. Once the button is clicked the selected address value is passed to the ICommand. Execute method. The CommandParameter is sent to both CanExecute and Execute events.

What is RelayCommand WPF?

The RelayCommand and RelayCommand<T> are ICommand implementations that can expose a method or delegate to the view. These types act as a way to bind commands between the viewmodel and UI elements.

What is command parameter in xamarin forms?

The ViewModel contains code associated with that ICommand property that is executed when the button is clicked. You can set CommandParameter to arbitrary data to distinguish between multiple buttons if they are all bound to the same ICommand property in the ViewModel.


1 Answers

Other than using the approach of defining properties in you class (let's call it your ViewModel) to be binded by your view, there are times (not common) where we don't wan't to do so, an important tool to know in these situations is the MultiBinding, so just for completeness sake , even though you are satisfied with the first option, I'll cover another approach.

so to answer your question:

1. MVVM Approach :

Use the MVVM approach and define properties to binded by your view, and use those properties in your ViewModel's command without the need for CommandParameters.

2. MultiBinding : (Can live happily with MVVM approach)

Passing the Command Parameter as a Multi Binded parameter as seen here:

 <Button Content="MultiBindingExample" Command="{Binding MyCommand}">
     <Button.CommandParameter>
         <MultiBinding Converter="{StaticResource MyMultiConverter}">
             <Binding Path="..." ElementName="MyTextBox"/>
             <Binding Path="..." ElementName="MySomethingElse"/>
         </MultiBinding>
     </Button.CommandParameter>
 </Button>

With your Converter Defined using the IMultiValueConverter Interface:

public class MyMultiConverter: IMultiValueConverter
{
    public object Convert(object[] values, ...)
    {
        return values.Clone();
    }
}

and for extracting the values: Simply refer to the parameter in your command as an Object[] and use the parameters in the same order as in the MultiBinding.

like image 124
Ron.B.I Avatar answered Sep 21 '22 08:09

Ron.B.I