gpt4 book ai didi

c# - 绑定(bind)到 List 的 ComboBox 的 MemberValue 是什么?

转载 作者:行者123 更新时间:2023-12-02 21:48:45 24 4
gpt4 key购买 nike

什么是 ValueMamber对于ComboBox绑定(bind)到 List<string>

我正在使用 Windows 窗体和 .NET Framework 4。

  cmbForms.DataSource = Forms;
cmbForms.ValueMember="System.String";
if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form))
{
cmbForms.SelectedValue = PhotoDescription.Details.Form;
}

哪里Forms是:

 public List<string> Forms { get; set; }

最佳答案

来自MSDN

If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object.

根据更新进行编辑

您的代码将收到 ArgumentException ,因为 System.String 不是可以解析的属性(您的 string 对象不没有名为 System.String 的属性)。默认值,从 MSDN ,将是一个空字符串 ("")

在这种情况下,您不需要设置 ValueMember 属性,因此可以使用 SelectedItem 代替。

cmbForms.DataSource = Forms;
if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form))
{
cmbForms.SelectedItem = PhotoDescription.Details.Form;
}

关于c# - 绑定(bind)到 List<string> 的 ComboBox 的 MemberValue 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19066132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com