开发者

WPF combobox value and display text

I'm used to doing things like

State.Items.Add(new ListItem { Text = "SomeState", Value = NumericIDofState });

Where State is a Listbox in ASP.NET.

How do i achieve the same with a WPF ComboBox? I do see a p开发者_如何转开发roperty called 'Content' in the ComboBoxItem object but how do i assign each item a value other than what's displayed to the user? Please help.


WPF Combobox has:

  • SelectedValuePath property that specifies the path to the property that is used to determine the value of the SelectedValue property. It's similar to ASP.NET ListItem's Value property.
  • DisplayMemberPath property that defines a default template that 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 behind
public 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"


See these properties of combo:

  • DisplayMemberPath
  • SelectedValuePath


If you skip the Value, then I think it's quite simple to add a new item into a ComboBox during runtime.

comboBox1.Items.Add("SomeText");

comboBox1.SelectedIndex = comboBox1.Items.Count - 1;

The SelectedIndex property is set to Items.Count-1 so that the newly added item appears in the ComboBox as the selected item.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜