简体   繁体   English

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

[英]C# Binding: How can I disable the CurrencyManager in BindingList so Current Item position is not maintained and not signaled?

I've got two ListBox'es that are databound to the same BindingList. 我有两个数据包到同一个BindingList的ListBox。

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. 问题是当从GUI更改所选项时,它正在改变BindingList中的位置,然后BindingList发信号通知另一个ListBox以更改其所选项。

So I've got the two ListBoxes Selected Item also synchronized which is not good for me. 所以我已经同步了两个ListBoxes Selected Item,这对我不利。

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): 示例代码(只需在设计时向表单添加两个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");
    }
}

Thanks, --Ran. 谢谢, - 兰。

将此行添加到Form_Load

this.listBox1.BindingContext = new BindingContext();

Declaring listBox1 and listBox2 to be of the following type seems to result in the desired behaviour. 将listBox1和listBox2声明为以下类型似乎会导致所需的行为。

class MyListBox: ListBox {

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

}

Regards, tamberg 此致,tamberg

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(); 我对此问题的解决方案是使用普通的List而不是BindingList,只需在Form对象上调用(更改前):this.BindingContext [Your List] .SuspendBinding(); and after the change to the List this.BindingContext[Your List].ResumeBinding(); 并且在更改为List之后.BindingContext [Your List] .ResumeBinding(); This updates all the bounded controls. 这会更新所有有界控件。

Notice it's also noted in the MSDN link here : 请注意, 这里也会在MSDN链接中注明:

"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." “如果绑定到未实现IBindingList接口的数据源(例如ArrayList),则在更新数据源时不会更新绑定控件的数据。例如,如果有一个绑定到ArrayList的组合框并且数据被添加到ArrayList中,这些新项目将不会出现在组合框中。但是,您可以通过在绑定控件的BindingContext类的实例上调用SuspendBinding和ResumeBinding方法来强制更新组合框。 “。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM