WPF combobox value and display text WPF combobox value and display text wpf wpf

WPF combobox value and display text


WPF Combobox has:

  • SelectedValuePath property that specifies the path to theproperty that is used to determine the value of the SelectedValueproperty. It's similar to ASP.NET ListItem's Value property.
  • DisplayMemberPath property that defines a default templatethat describes how to display the data objects. It's similar to ASP.NET ListItem's Text property.

Let's say you want your Combobox to show a collection of the following KeyValuePair objects:

private static readonly KeyValuePair<int, string>[] tripLengthList = {    new KeyValuePair<int, string>(0, "0"),    new KeyValuePair<int, string>(30, "30"),     new KeyValuePair<int, string>(50, "50"),     new KeyValuePair<int, string>(100, "100"), };

You define a property in your view model returning that collection:

public KeyValuePair<int, string>[] TripLengthList{    get    {        return tripLengthList;    }}

Then, your XAML for the Combobox would be:

<ComboBox    SelectedValue="{Binding FilterService.TripLengthFrom, Mode=TwoWay}"    ItemsSource="{Binding TripLengthList, Mode=OneTime}"    SelectedValuePath="Key"    DisplayMemberPath="Value" />

Where you set SelectedValuePath and DisplayMemberPath properties to the desired property names of the objects (Key and Value correspondingly) displaying by the Combobox.

Or, if you really want to add items to Combobox in code behind instead of using a binding, you can do it as well. For example:

<!--XAML--><ComboBox x:Name="ComboBoxFrom"    SelectedValue="{Binding FilterService.TripLengthFrom, Mode=TwoWay}" />// Code behindpublic partial class FilterView : UserControl{    public FilterView()    {        this.InitializeComponent();        this.ComboBoxFrom.SelectedValuePath = "Key";        this.ComboBoxFrom.DisplayMemberPath = "Value";        this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(0, "0"));        this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(30, "30"));        this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(50, "50"));        this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(100, "100"));    }


If you only want to expose a simple property in the viewmodel and handle the text for the choices in the view you can do a simple solution like this:

    <ComboBox SelectedValuePath="Tag" SelectedValue="{Binding YourIntProperty, Mode=TwoWay}">        <ComboBoxItem Content="First choice" Tag="0"/>        <ComboBoxItem Content="Second choice" Tag="1"/>        <ComboBoxItem Content="Third choice" Tag="2"/>    </ComboBox>

Example with a bool property:

    <ComboBox SelectedValuePath="Tag" SelectedValue="{Binding IsActive, Mode=TwoWay}">        <ComboBoxItem Content="No" Tag="False"/>        <ComboBoxItem Content="Yes" Tag="True"/>    </ComboBox>

Type-verbose alternatives (original examples)

Below are more verbose alternatives where the types are explicitly declared. Depending on your preferred style (or maybe some types that requires it), maybe it suits you better.

<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding YourIntProperty, Mode=TwoWay}">    <ComboBoxItem Content="First choice">        <ComboBoxItem.Tag>            <sys:Int32>0</sys:Int32>        </ComboBoxItem.Tag>    </ComboBoxItem>    <ComboBoxItem Content="Second choice">        <ComboBoxItem.Tag>            <sys:Int32>1</sys:Int32>        </ComboBoxItem.Tag>    </ComboBoxItem>    <ComboBoxItem Content="Third choice">        <ComboBoxItem.Tag>            <sys:Int32>2</sys:Int32>        </ComboBoxItem.Tag>    </ComboBoxItem></ComboBox>

Example with a bool property:

<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding IsActive, Mode=TwoWay}">    <ComboBoxItem Content="No">        <ComboBoxItem.Tag>            <sys:Boolean>False</sys:Boolean>        </ComboBoxItem.Tag>    </ComboBoxItem>    <ComboBoxItem Content="Yes">        <ComboBoxItem.Tag>            <sys:Boolean>True</sys:Boolean>        </ComboBoxItem.Tag>    </ComboBoxItem></ComboBox>

The sys namespace is declared as this:

xmlns:sys="clr-namespace:System;assembly=mscorlib"