C# 绑定:如何禁用 BindingList 中的CurrencyManager,以便不维护当前项目位置并且不发出信号?

发布于 2024-07-09 11:16:18 字数 1341 浏览 11 评论 0原文

我有两个 ListBox,它们的数据绑定到同一个 BindingList。

问题是,当从 GUI 更改所选项目时,它会更改 BindingList 中的位置,然后 BindingList 通知另一个 ListBox 更改其所选项目。

所以我已经同步了两个列表框选定的项目,这对我来说不好。

我想保持项目列表同步。 没有光标位置。

如何禁用该光标以使其不被维护?

示例代码(只需在设计时向表单添加两个列表框并注册 SelectedIndexChanged 事件并使用按钮注册按钮单击事件):

public partial class Form1 : Form
{
    BindingList<string> list = new BindingList<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        list.Add("bla1");
        list.Add("bla2");
        list.Add("bla3");

        this.listBox1.DataSource = list;
        this.listBox2.DataSource = list;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex != -1)
            System.Diagnostics.Trace.WriteLine("ListBox1: " + listBox1.SelectedItem.ToString());
    }

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox2.SelectedIndex != -1)
            System.Diagnostics.Trace.WriteLine("ListBox2: " + listBox2.SelectedItem.ToString());
    }

    // Register this event to a button
    private void button1_Click(object sender, EventArgs e)
    {
        list.Add("Test");
    }
}

谢谢, ——兰。

I've got two ListBox'es that are databound to the same BindingList.

The issue is that when changing the selected item from the GUI it's changing the position in the BindingList and then the BindingList signals the other ListBox to change its selected item.

So I've got the two ListBoxes Selected Item also synchronized which is not good for me.

I'd like to maintain the list of items in sync. without the cursor position.

How do I disable that cursor so it's not maintained?

sample code (just add two ListBoxes to the Form at design time and register the SelectedIndexChanged events and register the button click event with a button):

public partial class Form1 : Form
{
    BindingList<string> list = new BindingList<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        list.Add("bla1");
        list.Add("bla2");
        list.Add("bla3");

        this.listBox1.DataSource = list;
        this.listBox2.DataSource = list;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex != -1)
            System.Diagnostics.Trace.WriteLine("ListBox1: " + listBox1.SelectedItem.ToString());
    }

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox2.SelectedIndex != -1)
            System.Diagnostics.Trace.WriteLine("ListBox2: " + listBox2.SelectedItem.ToString());
    }

    // Register this event to a button
    private void button1_Click(object sender, EventArgs e)
    {
        list.Add("Test");
    }
}

Thanks,
--Ran.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箜明 2024-07-16 11:16:18

将此行添加到 Form_Load

this.listBox1.BindingContext = new BindingContext();

Add this line to Form_Load:

this.listBox1.BindingContext = new BindingContext();
神妖 2024-07-16 11:16:18

将 listBox1 和 listBox2 声明为以下类型似乎会产生所需的行为。

class MyListBox: ListBox {

    protected override void OnSelectedIndexChanged (EventArgs a) {
        if (DataManager != null) {
            DataManager.SuspendBinding();
        }
    }

}

问候,
坦贝格

Declaring listBox1 and listBox2 to be of the following type seems to result in the desired behaviour.

class MyListBox: ListBox {

    protected override void OnSelectedIndexChanged (EventArgs a) {
        if (DataManager != null) {
            DataManager.SuspendBinding();
        }
    }

}

Regards,
tamberg

喜爱纠缠 2024-07-16 11:16:18

我对此问题的解决方案是使用普通的 List 而不是 BindingList,并且只需在 Form 对象上调用(在更改之前):
this.BindingContext[您的列表].SuspendBinding();
更改列表后
this.BindingContext[您的列表].ResumeBinding();
这将更新所有有界控件。

请注意,此处的 MSDN 链接中也注明了这一点:

“如果你绑定的数据源没有实现IBindingList接口,比如ArrayList,当数据源更新时,绑定的控件的数据不会更新。例如,如果你绑定了一个组合框到 ArrayList 并将数据添加到 ArrayList 中,这些新项目将不会出现在组合框中,但是,您可以通过调用 BindingContext 类实例上的 SuspendBinding 和 ResumeBinding 方法来强制更新组合框。控件已绑定。”

My solution for this issue is to use a normal List instead of the BindingList and just call (before the change) on the Form object:
this.BindingContext[Your List].SuspendBinding();
and after the change to the List
this.BindingContext[Your List].ResumeBinding();
This updates all the bounded controls.

Notice it's also noted in the MSDN link here:

"If you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList, the bound control's data will not be updated when the data source is updated. For example, if you have a combo box bound to an ArrayList and data is added to the ArrayList, these new items will not appear in the combo box. However, you can force the combo box to be updated by calling the SuspendBinding and ResumeBinding methods on the instance of the BindingContext class to which the control is bound."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文