mkfl

The following code was obtained from: http://msdn2.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx

I may be missing something but how come the ConvertBack method does not need to account for the format type (ie "FormatLastFirst" or "FormatNormal") to return the values back to the binding sources properly Thanks.

public class NameConverter : IMultiValueConverter
{
  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  {
    string name;

    switch ((string)parameter)
    {
      case "FormatLastFirst":
        name = values[1] + ", " + values[0];
        break;
      case "FormatNormal":
      default:
        name = values[0] + " " + values[1];
        break;
    }

    return name;
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  {
    string[] splitValues = ((string)value).Split(' ');
    return splitValues;
  }
}


Re: Windows Presentation Foundation (WPF) IMultiValueConverter ConvertBack Question

Marlon Grech

I think it's just because the binding will update the source in the order the values where binded...






Re: Windows Presentation Foundation (WPF) IMultiValueConverter ConvertBack Question

mkfl

Oh I see but then wouldn't that mean there's a bug in the sample

The binding in the same sample above is as follows:

<MultiBinding Converter="{StaticResource myNameConverter}"
ConverterParameter="FormatLastFirst">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>

Since it's FormatLastFirst the value to be "split" in the converter would be say "Doe John". So the object array would be object [0] = Doe and object[1] = John, correct

FirstName = object[0] = Doe

LastName = object[1] = John





Re: Windows Presentation Foundation (WPF) IMultiValueConverter ConvertBack Question

Dr. WPF

You're right. That ConvertBack function is just wrong. It will also tag on a comma to the last name when the format is "FormatLastFirst". It's a bad example of a ConvertBack implementation for a multivalue converter.

Of course, the binding in the sample is effectively a OneWay binding since it's being set on a TextBlock, so the ConvertBack will never get called. But that's no excuse for sloppiness in the sample. You should submit a comment on that topic (click on "Click to Rate and Give Feedback" in the upper right corner of the page) and tell them to fix the code. Smile






Re: Windows Presentation Foundation (WPF) IMultiValueConverter ConvertBack Question

mkfl

I'm still learning so I wasn't sure if it was just my misinterpretation. Thanks for the reply & confirmation.