一个数据源可用于多个控件 [英] One DataSource for multiple controls

查看:82
本文介绍了一个数据源可用于多个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的winforms应用程序中有两个 ListBox ,我为他们两个分配了一个数据源,如下所示:

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;
}

奇怪的是(也许是因为这是我第一次!) ,在窗体中,当我单击其中一个lisbox的项目时,也会选择第二个列表框中的同一项目。这是默认行为吗?如何预防呢?如果这是默认行为,那么有什么用呢?

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)

推荐答案

列表框似乎缓存了绑定源。这是默认行为。如果要避免这种情况,最简单的方法是创建列表的副本以绑定到第二个数据源:

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:

lbxXunit.DataSource = units;
lbxYunit.DataSource = units.ToList();

当您对同一数据有多个视图并希望同步选择这些项目时,此功能很有用

This is useful when you have multiple views of the same data and want to synchronize the selection of these items.

这篇关于一个数据源可用于多个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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