简体   繁体   English

WPF组合框值和显示文本

[英]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. State是ASP.NET中的列表框。

How do i achieve the same with a WPF ComboBox? 我如何使用WPF ComboBox实现相同的目标? I do see a property called 'Content' in the ComboBoxItem object but how do i assign each item a value other than what's displayed to the user? 我确实在ComboBoxItem对象中看到了一个名为“Content”的属性,但是如何为每个项目分配一个值而不是显示给用户的值? Please help. 请帮忙。

WPF Combobox has: WPF Combobox有:

  • SelectedValuePath property that specifies the path to the property that is used to determine the value of the SelectedValue property. SelectedValuePath属性,指定用于确定SelectedValue属性值的属性的路径。 It's similar to ASP.NET ListItem 's Value property. 它类似于ASP.NET ListItemValue属性。
  • DisplayMemberPath property that defines a default template that describes how to display the data objects. DisplayMemberPath属性,用于定义描述如何显示数据对象的默认模板。 It's similar to ASP.NET ListItem 's Text property. 它类似于ASP.NET ListItemText属性。

Let's say you want your Combobox to show a collection of the following KeyValuePair objects: 假设您希望Combobox显示以下KeyValuePair对象的集合:

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的XAML将是:

<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 . SelectedValuePathDisplayMemberPath属性设置为Combobox显示的对象的所需属性名称( KeyValue )。

Or, if you really want to add items to Combobox in code behind instead of using a binding, you can do it as well. 或者,如果你真的想在后面的代码中添加项目到Combobox而不是使用绑定,你也可以这样做。 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: 如果您只想在viewmodel中公开一个简单的属性并处理视图中选项的文本,您可以执行如下简单的解决方案:

    <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: bool属性的示例:

    <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: bool属性的示例:

<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: sys命名空间声明为:

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

See these properties of combo: 请参阅组合的这些属性:

If you skip the Value, then I think it's quite simple to add a new item into a ComboBox during runtime. 如果跳过Value,那么我认为在运行时将新项添加到ComboBox中非常简单。

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. SelectedIndex属性设置为Items.Count-1以便新添加的项目作为选定项目显示在ComboBox中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM