Winforms ComboBox 中 SelectedValuePath 和 SelectedValue 的等价物是什么? [英] What is the equivalent of SelectedValuePath and SelectedValue in Winforms ComboBox?

查看:18
本文介绍了Winforms ComboBox 中 SelectedValuePath 和 SelectedValue 的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道从 WPF 组合框到 WinForms 的 SelectedValuePathselectedValue 的等价物是什么?我尝试使用 ValueMemberValue 但这不起作用...

Does someone know what is the equivalent of SelectedValuePath and selectedValue from WPF comboboxes to WinForms? I tried with ValueMember and Value but that doesn't work...

推荐答案

在 WPF 中,SelectedValuePath 获取或设置用于从SelectedItem.这正是 ValueMember 服务于 Windows Forms,它获取或设置属性的路径,用作 ComboBox 项目的实际值.

In WPF, SelectedValuePath gets or sets the path that is used to get the SelectedValue from the SelectedItem. It's exactly what ValueMember serves in Windows Forms, it gets or sets the path of the property to use as the actual value for the items of ComboBox.

在 windows 窗体中,当您想使用 ComboBox 进行数据绑定时,您应该使用以下属性:

In windows forms when you want to use data-binding with a ComboBox, you should use this properties:

  • 数据源
    实现 IList 接口或 Array 的对象.
  • <代码>显示成员
    DataSource 属性指定的集合中包含的对象属性的名称.如果对象上不存在指定的属性,或者 DisplayMember 的值为空字符串 (""),则显示对象的 ToString 方法的结果.
  • ValueMember
    表示 DataSource 属性值的单个属性名称,或解析为最终数据绑定对象的属性名称的以句点分隔的属性名称的层次结构.

  • DataSource
    An object that implements the IList interface or an Array.
  • DisplayMember
    The name of an object property that is contained in the collection specified by the DataSource property. If the specified property does not exist on the object or the value of DisplayMember is an empty string (""), the results of the object's ToString method are displayed instead.
  • ValueMember
    epresenting a single property name of the DataSource property value, or a hierarchy of period-delimited property names that resolves to a property name of the final data-bound object.

SelectedValue
一个对象,包含由 ValueMember 属性指定的数据源成员的值.

SelectedValue
An object containing the value of the member of the data source specified by the ValueMember property.

示例

在你的表单上放置一个 ComboBox 和一个 Button 并处理 FormLoad 事件Button 的点击 事件如下.通过点击Button,您会看到ComboBox 的选中项将变为Two.不要忘记为事件注册事件处理程序.

Put a ComboBox and a Button on your form and handle Load event of Form and Click event of Button like below below. By click on Button you will see the selected item of ComboBox will be changed to Two. Don't forget to register event handlers for events.

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

private void Form1_Load(object sender, EventArgs e)
{
    var categories = new List<Category>()
    {
        new Category(){Id=1, Name= "One"},
        new Category(){Id=2, Name= "Two"},
        new Category(){Id=3, Name= "Three"},
    };

    this.comboBox1.DataSource = categories;
    this.comboBox1.DisplayMember = "Name";
    this.comboBox1.ValueMember = "Id";
}

private void button1_Click(object sender, EventArgs e)
{
    this.comboBox1.SelectedValue = 2;
}

这篇关于Winforms ComboBox 中 SelectedValuePath 和 SelectedValue 的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆