I am writing a WPF MVVM Application. I am trying to use a converter to display a contact name in the combobox. I do not think I can use the DisplayMemberPath since a "fullname" column does not exist.
The ComboBox is being bound to a class within a class using Entity Framework. Given the following:
.cs Files
public class Car
{
public int CarId { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public string Year { get; set; }
public Contact Owner { get; set; }
}
public class Contact
{
public int ContactID { get; set; }
public string Salutation { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Suffix { get; set; }
}
public class MultiBindingConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string name = "";
if (!(values[0] is Contact Contact))
return name;
switch ((string)parameter)
{
case "LastNameFirst":
name += (!string.IsNullOrEmpty(Contact.LastName)) ? Contact.LastName : "";
name += (!string.IsNullOrEmpty(Contact.Suffix)) ? " " + Contact.Suffix : "";
name += (!string.IsNullOrEmpty(Contact.FirstName)) ? ", " + Contact.FirstName : "";
name += (!string.IsNullOrEmpty(Contact.MiddleName)) ? " " + Contact.MiddleName : "";
name += (!string.IsNullOrEmpty(Contact.Salutation)) ? ", " + Contact.Salutation : "";
break;
case "FormatNormal":
default:
name += (!string.IsNullOrEmpty(Contact.Salutation)) ? Contact.Salutation : "";
name += (!string.IsNullOrEmpty(Contact.FirstName)) ? " " + Contact.FirstName : "";
name += (!string.IsNullOrEmpty(Contact.MiddleName)) ? " " + Contact.MiddleName : "";
name += (!string.IsNullOrEmpty(Contact.LastName)) ? " " + Contact.LastName : "";
name += (!string.IsNullOrEmpty(Contact.Suffix)) ? " " + Contact.Suffix : "";
break;
}
return name;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
.XAML file
<UserControl.Resources>
<local:MultiBindingConverter x:Key="MBC" />
</UserControl.Resources>
<ComboBox ItemsSource="{Binding Contacts, Mode=OneTime}" // Contacts is a full list of the Contact Class (so its every Owner)
SelectedValuePath="ContactId"
SelectedValue="{Binding Car.Owner.ContactId, Mode=TwoWay}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MBC}" ConverterParameter="LastNameFirst" >
<Binding Path="Contacts"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The issue is that the end result that is displayed in the ComboBox is:
System.Data.Entity.DynamicProxies.Contact_.......
It is not showing the Owner name in the correct format. How can I do a binding to the ComboBox in this manner to get the output I desire (ie. Doe Sr., John Michael, Mr.)
EDIT I have also tried it this way
.cs IValueConverter
public class ContactNameConverter : BaseValueConverter<ContactNameConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string name = "";
if (!(value is Contact Contact))
return name;
switch ((string)parameter)
{
case "LastNameFirst":
name += (!string.IsNullOrEmpty(Contact.LastName)) ? Contact.LastName : "";
name += (!string.IsNullOrEmpty(Contact.Suffix)) ? " " + Contact.Suffix : "";
name += (!string.IsNullOrEmpty(Contact.FirstName)) ? ", " + Contact.FirstName : "";
name += (!string.IsNullOrEmpty(Contact.MiddleName)) ? " " + Contact.MiddleName : "";
name += (!string.IsNullOrEmpty(Contact.Salutation)) ? ", " + Contact.Salutation : "";
break;
case "FormatNormal":
default:
name += (!string.IsNullOrEmpty(Contact.Salutation)) ? Contact.Salutation : "";
name += (!string.IsNullOrEmpty(Contact.FirstName)) ? " " + Contact.FirstName : "";
name += (!string.IsNullOrEmpty(Contact.MiddleName)) ? " " + Contact.MiddleName : "";
name += (!string.IsNullOrEmpty(Contact.LastName)) ? " " + Contact.LastName : "";
name += (!string.IsNullOrEmpty(Contact.Suffix)) ? " " + Contact.Suffix : "";
break;
}
return name;
}
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
.XAML
<ComboBox ItemsSource="{Binding Contacts, Mode=OneTime}"
SelectedValuePath="ContactId"
SelectedValue="{Binding Car.Owner.ContactId, Mode=TwoWay}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., Converter={local:ContactNameConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>