多个控件的一个数据源
我的 winforms 应用程序中有两个 ListBox
,我为它们分配了一个数据源,如下所示:
private void MakeMeasurementUnits()
{
var units = new List<MeasurementUnit>
{
new MeasurementUnit {Name = "Current", SiUnit = "A"},
new MeasurementUnit {Name = "Voltage", SiUnit = "V"},
new MeasurementUnit {Name = "Time", SiUnit = "s"},
new MeasurementUnit {Name = "Temprature", SiUnit = "°C"}
};
lbxXunit.DataSource = units;
lbxYunit.DataSource = units;
}
奇怪的是(或者可能因为这是我第一次!!),在我单击时的表单中在这些列表框之一的项目上,第二个列表框中的相同项目也会被选择。这是默认行为吗?如何防止这种情况?如果这是默认行为,那么它有什么用处呢?
我发现快速补救措施是制作两个不同的数据源(相同的东西但名称不同)
I have two ListBox
in my winforms application, I assigne a datasource for both of them as follow:
private void MakeMeasurementUnits()
{
var units = new List<MeasurementUnit>
{
new MeasurementUnit {Name = "Current", SiUnit = "A"},
new MeasurementUnit {Name = "Voltage", SiUnit = "V"},
new MeasurementUnit {Name = "Time", SiUnit = "s"},
new MeasurementUnit {Name = "Temprature", SiUnit = "°C"}
};
lbxXunit.DataSource = units;
lbxYunit.DataSource = units;
}
The strange thing is (or maybe because it is my first time!!), in the form when I click on items of one of these lisboxes, the same item in the second listbox gets selected as well. Is this a default behaviour? how to prevent this? If this is default behaviour, what is useful about it?
I found the quick remedy to be making two different datasources (same thing with another name)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
列表框似乎缓存了绑定源。这是默认行为。如果您想避免这种情况,简单的方法是创建列表的副本以绑定到第二个数据源:
当您拥有同一数据的多个视图并希望同步这些项目的选择时,这非常有用。
The listbox seems to cache the binding source. This is default behavior. If you want to avoid this, the easy way is to create a copy of the list to bind to the second data source:
This is useful when you have multiple views of the same data and want to synchronize the selection of these items.
是的,这是正常行为。发生这种情况是因为 ListView 控件使用 BindingSource 对象来跟踪当前选定的项目。 (列表无法在没有 BindingSource 的情况下跟踪所选项目。)
默认情况下,WinForms 控件中的 DataSource 使用 WinForms 系统本身为其创建的 BindingSource。
您可以在以下位置阅读有关 BindingSource 的更多信息:
http://msdn.microsoft.com/en-us /library/system.windows.forms.bindingsource.aspx
这里有一篇文章也可能有帮助:
http://blogs.msdn.com/b/bethmassi/archive/2007/09/19/binding-multiple-comboboxes-to-the-same-datasource.aspx
Yes, this is normal behaviour. It happens because the ListView control uses a BindingSource object to track the currently selected item. (A List has no way to track a selected item without a BindingSource.)
By default, a DataSource in a WinForms control uses a BindingSource created for it by the WinForms system itself.
You can read more about the BindingSource at:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx
There is an article here which might help too:
http://blogs.msdn.com/b/bethmassi/archive/2007/09/19/binding-multiple-comboboxes-to-the-same-datasource.aspx
您注意到的行为是 winforms 控件的默认/正确行为。您可以通过为第二个列表框控件设置新的 BindingContext 来实现您的目标,而无需创建数据源的副本。
BindingContext
The behavior you have noted is the default/correct behavior for winforms controls. You can achieve what you are after by setting a new BindingContext for your second listbox control without creating a copy of your data source.
BindingContext
这是正确的行为。
WindowsForms
中的数据源
管理跟踪控件上的所选项目并操作绑定数据。您已经找到的解决方案是:为这些控件分配 2 个不同的
数据源对象
。This is correct behaviour. The
datasource
management inWindowsForms
keeps track of the selected item on control and manipulates binded data too.The resolution you've found already: is assign 2 different
data sources objects
to these controls.