Hi,
I've a Person class
...and a list of PersonCode:public class Person { public string location; public string name; ... }
In a ComboBox, I want to display this list formated that way: name (location). So I've used a Converter and it's working fine until there:Code:List<Person> list1 = new List<Person>() { new Person() { location = "Chicago", name = "Michael" }, new Person() { location = "", name = "Mario" }, new Person() { location = "Denver", name = "Michael" } };
Code:<ComboBox ItemsSource="{Binding list1}" IsSynchronizedWithCurrentItem="True"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Converter={StaticResource concatNameAndLoc}}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>Code:public class ConcatNameAndOption : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Person valConv = TryCast(value, Person); string loc = valConv.Location; if ((loc != "")) { loc = (" (" + (loc + ")")); } return (valConv.Name + loc); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } }
I can see the data displayed correctly in my ComboBox:
Michael (Chicago)
Mario
Michael (Denver)
Now, here is my problem:
The selected Person will be savec in a XML file (<Person location="Paris">Michel</Person>) and I would like, at loading, that the ComboBox selects this Person (the one with the matching Name and Location concatenation)
...in other words, from the concatenation on the 'saved' person, I want to find the corresponding concatenated person in the list.
How to bind the SelectedValue? I've tryed SelectedValue="{Binding Person, Converter={StaticResource concatNameAndOpt}, NotifyOnSourceUpdated=True}"
...but without succes.
Thanks for reading me! Hope that I was clear!


Reply With Quote