Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a CombBox which binded to a List<cusomer> as itemsource.When the user opens the drop down I have created a DataTemplate to show all the details of the customer(FirstName LastName Age County). But when the user selects any item, in the display text I want to show the customer FirstName and Last Name. Can anyone tell me how to achieve this using Styles.
Posted

1 solution

First of all you need a model class for customer:
Customer.cs
C#
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:
C#
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:
XML
<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:
C#
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
C#
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
    }
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900