将 ComboBox DataSource 设置为枚举并绑定 SelectedValue

Jan*_*nik 0 c# data-binding combobox winforms

我有点绝望,因为昨天它可以工作,但今天这不再有效,让我解释一下,我正在尝试做什么:

我想设置DataSourceComboBox所有枚举值在一个特定的枚举。就像这样:

cmbType.DataSource = m_addViewPresenter.Types;
Run Code Online (Sandbox Code Playgroud)

其中 Types 是 aBindingList并且正在像这样初始化:

public BindingList<MyEnum> Types
{
    get { return m_types; }
    set
    {
        m_types = value;
        OnPropertyChanged();
    }
}

[ImportingConstructor]
public AddViewPresenter()
{
    Types = new BindingList<MyEnum>(
              Enum.GetValues(typeof(MyEnum))
                         .Cast<MyEnum>().ToList());
}
Run Code Online (Sandbox Code Playgroud)

此外,我想将选择的当前项目绑定到一个属性。由于ComboBox.SelectedItem不触发 -INotifyProperty事件,我使用ComboBox.SelectedValue.

cmbType.Bind(m_addViewPresenter, c => c.SelectedValue, m => m.SelectedValue);

public static void Bind<TComponent, T>(
    this TComponent component, T value,
    Expression<Func<TComponent, object>> controlProperty, 
    Expression<Func<T, object>> modelProperty)
    where TComponent : IBindableComponent
    where T : INotifyPropertyChanged
{
    var controlPropertyName = PropertyNameResolver.GetPropertyName(controlProperty);
    var modelPropertyName = PropertyNameResolver.GetPropertyName(modelProperty);
    component.DataBindings.Add(new Binding(controlPropertyName, value, modelPropertyName, false, DataSourceUpdateMode.OnPropertyChanged));
}
Run Code Online (Sandbox Code Playgroud)

昨天它运行良好,但有些事情搞砸了,今天我只得到一个InvalidOperationException

无法在具有空 ValueMember 的 ListControl 中设置 SelectedValue。

我知道这是在黑暗中挖掘,但有人能和我一起集思广益并找出问题所在吗?提前致谢!

Rez*_*aei 5

选项1

SelectedValue用于数据绑定,请创建一个 calss DataItem:

public class DataItem
{
    public MyEnum Value { get; set; }
    public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后使用此代码进行数据绑定:

this.comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>()
                                .Select(x => new DataItem() { Value = x, Text = x.ToString() })
                                .ToList();
this.comboBox1.ValueMember = "Value";
this.comboBox1.DisplayMember = "Text";

this.comboBox1.DataBindings.Add(
    new Binding("SelectedValue", yourObjectToBind, "PropertyOfYourObject"));
Run Code Online (Sandbox Code Playgroud)

您可以制作通用DataItem<T>包含public T Value { get; set; }以使其在您的项目中更可重用。

选项 2

要使用SelectedItem数据绑定:

this.comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
this.comboBox1.DataBindings.Add(
    new Binding("SelectedItem", yourObjectToBind, "PropertyOfYourObject"));
Run Code Online (Sandbox Code Playgroud)

选项 3

要将SelectedValue数据绑定用作 Ivan Stoev 建议的另一个选项,您可以通过以下方式执行数据绑定:

this.comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
this.comboBox1.DataBindings.Add(
    new Binding("SelectedValue", yourObjectToBind, "PropertyOfYourObject",
                true,  DataSourceUpdateMode.OnPropertyChanged));
Run Code Online (Sandbox Code Playgroud)

詹尼克编辑:

选项 1 的基本通用方法如下所示:

public class ComboBoxItemWrapper<T>
{
    public T Value { get; set; }
    public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)