Sunday, July 18, 2010

WPF Converter values


I posted this at stackoverflow : http://stackoverflow.com/questions/3204422/wpf-corelating-multibindings-and-converters-values

The problem was how do you handle indexes when your converter value array is of large size. I thought over it and come up with this:

https://www.assembla.com/code/TechnologyAndMe/subversion/nodes/ConverterValues
(Download complete source using a SVN client from http://subversion.assembla.com/svn/TechnologyAndMe/ConverterValues/)

So now all you have to do is :









Here Prop1,Prop2,Prop3 are the actual values that you want to pass to the converter, Value1, Value2, Value3 are just ids for Properties below them. Now in the converter, just use my BoundValues class and accessing the properties become as easy as saying :

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var boundValues = new BoundValues(values);

return string.Format("{0} {1} {2}",
boundValues["Value1"], boundValues["Value2"], boundValues["Value3"]);
}

Where BoundValues["Value1"] returns value of Prop1, so now I no longer have to worry about managing the sequence of bindings in my XAML.

No comments: