20

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
}
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Mandar Jogalekar
  • 3,199
  • 7
  • 44
  • 85
  • if you describe what exact parameters you want, their types, and why, we might be able to help. There is only one "binding" for a command parameter, but there are other options depending on what you need, so more info please. – J King Apr 11 '13 at 15:30
  • You can transform many parameters in a single one, using an object[] (object array) for example, but then you need to cast all those parameters aftwerwards. Or is there a problem with that? – Master117 Apr 11 '13 at 15:38
  • hi J King, it would another control . a textbox can be good assumption. – Mandar Jogalekar Apr 11 '13 at 16:19
  • I agree with reed's answer, without knowing exactly what you need the textbox for, the best approach is to use the bound properties in the viewmodel in the command execute. Otherwise if the textbox is part of a listbox, datagrid, combobox, etc then you could pass the selected item of the control which would give you more options. – J King Apr 12 '13 at 05:02

2 Answers2

34

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.

Ron.B.I
  • 2,726
  • 1
  • 20
  • 27
  • 2
    your converter returns proper values, but in the method attached to the relay command returns an array, but array elements are null. – Alex David Mar 06 '14 at 03:28
  • Have you defined the static resource correctly? please see: http://msdn.microsoft.com/en-us/library/windows/apps/hh758287.aspx for further information, focus on the stackpanel example, where you can see how to define a static resource. – Ron.B.I Mar 06 '14 at 08:35
  • I am facing same issue, everything is perfect except the values are null. – Amit Oct 05 '14 at 14:10
  • 3
    Replace return values; to return values.Clone(); in the convert of MyMultiConverter and it should work fine. – Ali Baig Mar 12 '15 at 23:36
15

How can i send multiple parameters from button in wpf.

You can only send one parameter as the CommandParameter.

A better solution is typically to just bind the TextBox and other controls to multiple properties in your ViewModel. The command would then have access to all of those properties (since it's in the same class), with no need for a command parameter at all.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373