First of all you need a model class for customer:
Customer.cs
namespace WpfApplication
{
class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Country { get; set; }
}
}
For converting a customer object to "FirstName LastName"
you need a value converter class:
CustomerConverter.cs:
using System;
using System.Windows.Data;
namespace WpfApplication
{
class CustomerConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Customer customer = value as Customer;
return string.Format("{0} {1}", customer.FirstName, customer.LastName);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
MainWindow.xaml:
Here you define an object of the value converter
and define it as converter in the list box item template:
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:CustomerConverter x:Key="CustomerConv"></local:CustomerConverter>
</Window.Resources>
<Grid>
<ComboBox ItemsSource="{Binding Customers}" Width="300" Height="30" Margin="10" VerticalAlignment="Top">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource CustomerConv}}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace WpfApplication
{
public partial class MainWindow : Window
{
ViewModel vm;
public MainWindow()
{
this.Loaded += MainWindow_Loaded;
InitializeComponent();
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
vm = new ViewModel();
this.DataContext = vm;
}
}
}
And of course you need a ViewModel.
ViewModel.cs
namespace WpfApplication
{
class ViewModel : INotifyPropertyChanged
{
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
public sealed class CallerMemberNameAttribute : Attribute { }
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#region Constructors
public ViewModel()
{
customers = new ObservableCollection<Customer>();
Customers.Add(new Customer() { FirstName = "Bill", LastName = "Clinton", Country = "USA", Age = 60 });
Customers.Add(new Customer() { FirstName = "Angela", LastName = "Merkel", Country = "Germany", Age = 60 });
Customers.Add(new Customer() { FirstName = "Vladimir", LastName = "Putin", Country = "Russia", Age = 60 });
}
#endregion
#region Fields
ObservableCollection<Customer> customers;
#endregion
#region Properties
public ObservableCollection<Customer> Customers
{
get
{
return customers;
}
set
{
if (customers != value)
{
customers = value;
OnPropertyChanged();
}
}
}
#endregion
}
}