Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForms-在运行时更改控件的bindingsource/datasource?_C#_Winforms_Data Binding - Fatal编程技术网

C# WinForms-在运行时更改控件的bindingsource/datasource?

C# WinForms-在运行时更改控件的bindingsource/datasource?,c#,winforms,data-binding,C#,Winforms,Data Binding,我有一个带有BindingSource组件的Winforms应用程序,其数据源设置为我为自定义数据对象创建的数据源。表单上还有几个控件,它们绑定到通过BindingSource公开的对象的各种属性。其中许多控件都是组合框,将显示带有支持枚举的值,因此我为这些控件设置数据源,如下所示: comboBox1.DataSource = new BindingSource(Utility.ToList(typeof(DataObject.EnumValues)), null);

我有一个带有BindingSource组件的Winforms应用程序,其数据源设置为我为自定义数据对象创建的数据源。表单上还有几个控件,它们绑定到通过BindingSource公开的对象的各种属性。其中许多控件都是组合框,将显示带有支持枚举的值,因此我为这些控件设置数据源,如下所示:

        comboBox1.DataSource = new BindingSource(Utility.ToList(typeof(DataObject.EnumValues)), null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";
        comboBox2.DataBindings.Add(new Binding("SelectedValue", this.bindingSource, "PropertyName1", true));
        comboBox2.DataSource = new BindingSource(Utility.ToList(typeof(DataObject.FirstSetOfEnumValues)), null);
        comboBox2.DisplayMember = "Value";
        comboBox2.ValueMember = "Key";
这一切都很好,但我有两个组合框,我需要能够在运行时更改它们以显示其他值(使用不同的后枚举)。在这两种情况下,我将用如下代码创建初始绑定和数据源:

        comboBox1.DataSource = new BindingSource(Utility.ToList(typeof(DataObject.EnumValues)), null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";
        comboBox2.DataBindings.Add(new Binding("SelectedValue", this.bindingSource, "PropertyName1", true));
        comboBox2.DataSource = new BindingSource(Utility.ToList(typeof(DataObject.FirstSetOfEnumValues)), null);
        comboBox2.DisplayMember = "Value";
        comboBox2.ValueMember = "Key";
…然后,当我需要comboBox2绑定并显示不同的值时,我会执行以下操作:

        comboBox2.DataBindings.Clear();
        comboBox2.DataBindings.Add(new Binding("SelectedValue", this.bindingSource, "PropertyName2", true));
        comboBox2.DataSource = null;
        comboBox2.DataSource = new BindingSource(Utility.ToList(typeof(DataObject.SecondSetOfEnumValues)), null);
        comboBox2.DisplayMember = "Value";
        comboBox2.ValueMember = "Key";

据我所知,这是正常工作,但它的丑陋,必须有一个更好的方法来做到这一点,对吗?如果你知道它是什么,我很想听!非常感谢

如果这是一个web表单,我可能会建议将ComboBox2作为两个单独的组合框,并隐藏/显示所需的组合框。尽管我知道这对于WinForm项目来说并不容易,除非您使用的是流动布局

您可以添加一个函数,根据枚举类型返回数据源。。。我认为在调用Clear()之后不需要重新设置DisplayMember和ValueMember属性(但我可能错了)


除此之外,我不认为你能把它简化得更多。尽管我很高兴听到有人有更好的解决方案:)

如果这是一个web表单,我可能会建议将ComboBox2作为两个单独的组合框,并隐藏/显示您需要的一个。尽管我知道这对于WinForm项目来说并不容易,除非您使用的是流动布局

您可以添加一个函数,根据枚举类型返回数据源。。。我认为在调用Clear()之后不需要重新设置DisplayMember和ValueMember属性(但我可能错了)


除此之外,我不认为你能把它简化得更多。尽管我很高兴听到有人有更好的解决方案:)

您不需要将组合框绑定到BindingSource的新实例

将组合框绑定到各自的绑定源。这可以通过Windows窗体设计器完成,也可以在自己的代码中手动完成。如果您在代码中这样做,请确保保留对BindingSources的引用。如果使用设计器,则会将成员添加到表单类中

然后,当您想要显示一组不同的值时,只需更改BindingSources上的数据源,组合框就会相应地更新


干杯

您不需要将组合框绑定到BindingSource的新实例

将组合框绑定到各自的绑定源。这可以通过Windows窗体设计器完成,也可以在自己的代码中手动完成。如果您在代码中这样做,请确保保留对BindingSources的引用。如果使用设计器,则会将成员添加到表单类中

然后,当您想要显示一组不同的值时,只需更改BindingSources上的数据源,组合框就会相应地更新

干杯