Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
<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:
C#
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());
            // Exception text is not in correct format!
        }
        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:
C#
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);

            // Designer Crashes with Unreported Exception in the following line.
            return new Product(s);
            // Application works but the teamLead says the designer should work I must resolve this issue...
        }
        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?
Posted
Comments
Fredrik Bornander 12-Mar-12 13:23pm    
That code works fine for me, I get one popup on each lostfocus of the text boxes.
Can you show how your txtID is defined?

If you just need the designer to work, you can add a check in the converter to see if you're currently in Designer mode;

class PVM : IMultiValueConverter
{
  private static readonly DependencyObject DummyDependencyObject = new DependencyObject();
        
  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];
      if (DesignerProperties.GetIsInDesignMode(DummyDependencyObject))
        return null;
      else
        return s;
    }
    return null;
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  {
    return null;
  }
}


Hope this helps,
Fredrik
 
Share this answer
 
Well the main objective would be to determine:
1.) why do I see more Messageboxes instead of just one
2.) and I guess that would resolve how to correct the error.
 
Share this answer
 
Comments
Fredrik Bornander 13-Mar-12 5:21am    
Like I said, It works for me so I can't tell you what's wrong without seeing the how txtID is declared.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900