C# 如何将文本框绑定到BindingList中特定项的特定字段?

C# 如何将文本框绑定到BindingList中特定项的特定字段?,c#,winforms,data-binding,inotifypropertychanged,bindinglist,C#,Winforms,Data Binding,Inotifypropertychanged,Bindinglist,文本框是否可以绑定到BindingList中特定项的特定字段 下面是一段代码来说明该任务: public class MyItemClass : INotifyPropertyChanged { // ... } // ... var item1 = MyItemClass { MyItemProperty1 = 123, MyItemProperty2 = "456" }; var item2 = MyItemClass { MyItemProperty1 = 789, MyIt

文本框是否可以绑定到BindingList中特定项的特定字段

下面是一段代码来说明该任务:

public class MyItemClass : INotifyPropertyChanged
{
    // ...
}

// ...

var item1 = MyItemClass { MyItemProperty1 = 123, MyItemProperty2 = "456" };

var item2 = MyItemClass { MyItemProperty1 = 789, MyItemProperty2 = "012" };

var bindingList = new BindingList<MyItemClass>();

bindingList.Add(item1);

bindingList.Add(item2);

var myFirstItemProperty2TextBox = new TextBox();

var my789ItemProperty2TextBox = new TextBox();

// myFirstItemProperty2TextBox.Text property
// should be bound to the MyItemProperty2 property of the very
// first item in bindingList if possible

// my789ItemProperty2TextBox.Text property
// should be bound to the MyItemProperty2 property of the
// first (or every) item in bindingList that has MyItemProperty1 property
// equal to "789" if possible

// In case the list item gets removed and another takes its place
// the text boxes should be re-bound to the new items
// that fits the criterion if possible

// Introducing additional helper controls/variables/delegates
// in between the list and the text boxes is acceptable if needed
// (not too much preferably)
公共类MyItemClass:INotifyPropertyChanged
{
// ...
}
// ...
var item1=MyItemClass{MyItemProperty1=123,MyItemProperty2=“456”};
var item2=MyItemClass{MyItemProperty1=789,MyItemProperty2=“012”};
var bindingList=新的bindingList();
bindingList.Add(item1);
bindingList.Add(第2项);
var myFirstItemProperty2TextBox=新文本框();
var my789ItemProperty2TextBox=新文本框();
//myFirstItemProperty2TextBox.Text属性
//应该绑定到
//如果可能,bindingList中的第一项
//my789ItemProperty2TextBox.Text属性
//应绑定到的MyItemProperty2属性
//bindingList中具有MyItemProperty1属性的第一个(或每个)项
//如果可能,等于“789”
//如果该列表项被删除,另一个列表项取代
//文本框应重新绑定到新项目
//如果可能的话,这符合标准
//引入其他辅助控件/变量/委托
//如果需要,可以在列表和文本框之间选择
//(最好不要太多)
这可能吗?如果是,如何进行

事实上,我几乎可以肯定这是可以实现的(至少在某种中间逻辑的帮助下),但事实上,问题在于找到一种相当简单、高效和可靠的方法


我对绑定组合框(使用DisplayMember/ValueMember)也感兴趣。

尝试使用控件的名称和/或标记属性。将名称字符串设置为公式化,就像处理变量名一样,然后可以使用内置方法或一点Linq在控件树中再次找到它,并且可以动态绑定/取消绑定其文本属性。真正的“数据绑定”更像是一个WPF概念,但您几乎可以轻松地实现动态WinForms。@KeithS WPF的致命问题在于它不是跨平台的(WinForms可以在Mono上运行,WPF不能)。至于把名字设置成“公式化的东西”——我并不打算把它作为运行时逻辑的一部分,但我会考虑这一点。谢谢