C# 绑定:如何禁用 BindingList 中的CurrencyManager,以便不维护当前项目位置并且不发出信号?
我有两个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将此行添加到
Form_Load
:Add this line to
Form_Load
:将 listBox1 和 listBox2 声明为以下类型似乎会产生所需的行为。
问候,
坦贝格
Declaring listBox1 and listBox2 to be of the following type seems to result in the desired behaviour.
Regards,
tamberg
我对此问题的解决方案是使用普通的 List 而不是 BindingList,并且只需在 Form 对象上调用(在更改之前):
this.BindingContext[您的列表].SuspendBinding();
更改列表后
this.BindingContext[您的列表].ResumeBinding();
这将更新所有有界控件。
请注意,此处的 MSDN 链接中也注明了这一点:
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: