Hi All
I have an Oberserveable Collection of Type Student. The Student Class has properties First Name and Last name. Initally, I used to show just the First Name in the Combo Box for which I used following bindings.
<ComboBox Grid.Row="3" Grid.Column="1" Name="cboStudent" ItemsSource="{Binding Path=StudentCollection}" DisplayMemberPath="FirstName" SelectedValue="{Binding Path=CurrentStudent,Mode=TwoWay}"></ComboBox>Please note that I am binding the selected Item to CurrentStudent
But Now that i needed to show the fullname in the combobox, I used a IValueConverter.
class CStudentFormatter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//.Count ==0;
if ((value as System.Collections.ObjectModel.ObservableCollection<Student>).Count()==0)
{
return 1;
}
System.Collections.ObjectModel.ObservableCollection<Student> k = (value as System.Collections.ObjectModel.ObservableCollection<Student>);
List<string> returnValue = new List<string>();
foreach (var item in k)
{
returnValue.Add(string.Format("{0} ({1})", item.FirstName, item.LastName));
}
return returnValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}I reformatted by orginal combobox binding as following.
<ComboBox Grid.Row="7" Grid.Column="5"
ItemsSource="{Binding Path=StudentCollection,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged , Converter={StaticResource CStudentFormatter}}"></ComboBox>Now, I am able to get the desired Full name in the combo box , but I am not able to get the selected Item back to the Current Student on selection. I tried following
SelectedItem="{Binding Path=CurrentStudent, Converter={StaticResource CStudentFormatter}}Can anyone please help me ?
Thanks in Advance.
Anu
Anu Viswan : www.AnuViswan.blogspot.com