Hi,
I have encountered a major problem with MultiBinding as Button.CommandParameter. In my application I have four textBoxes which contents should be used as a parameter for the Button.Command. For this purpose I've done something like this inn the XAML file:
<Button Grid.Row="0" Grid.Column="2" x:Name="btnUpdate" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="5,40,5,0" Content="Update product" Command="{Binding Path=Update_Command}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource ResourceKey=PVMObj}">
<Binding ElementName="txtID" Path="Text" /> <!-- TextBox with the ID -->
<Binding ElementName="txtName" Path="Text" /> <!-- TextBox with the Name -->
<Binding ElementName="txtPrice" Path="Text" /> <!-- TextBox with the Price -->
<Binding ElementName="txtReseller" Path="Text" /> <!-- TextBox with the reseller name -->
</MultiBinding>
<Button.CommandParameter>
</Button>
As you can see I have the necessary MultiValueConverter Object (PVMObj) bound to MultiBinding. And the PVM's implementation looks like this:
class PVM : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(values != null)
{
return new Product(ToInt32(values[0]), values[1].ToString(), ToDouble(values[2]), values[3].ToString());
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
#endregion
}
For debugging purposes I've modified the PMV as follows:
class PVM : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(values != null && values[3].ToString().Length > 0)
{
string s = values[0]+"-"+values[1]+"-"+values[2]+"-"+values[3];
System.Windows.MessageBox.Show(s);
return new Product(s);
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
#endregion
}
In this case the following scenario occurs: First a Messagebox appears with the following contents:
"1- - - "
Then I close this MessageBox and another one pops up:
"1-Sample- - "
then again
"1-Sample-19.95- "
And for last time:
"1-Sample-19.95-MediaTower"
How does this IMultiValueConverter works? From many examples as I saw people are simply using the object[] values array, yet I can not. Any thoughts what I am doing wrong?