[英]Winforms, databinding, Listbox and textbox
I have a ListBox ( MyListBox
) on my screen, and a Textbox ( MyTextBox
).我的屏幕上有一个列表框 (
MyListBox
) 和一个文本框 ( MyTextBox
)。
The ListBox is filled with a List(Of T), which are all custom items. ListBox 中填充了一个 List(Of T),它们都是自定义项。
Now I try to do this:现在我尝试这样做:
The ListBox' datasource is the List(Of T). ListBox 的数据源是 List(Of T)。
Now when an Item changes I want the textbox to be updated to a particular property of the selected item in my ListBox.现在,当项目更改时,我希望将文本框更新为列表框中所选项目的特定属性。
In code this means:在代码中,这意味着:
Me.MyListBox.DisplayMember = "SelectionName"
Me.MyListBox.ValueMember = "Id"
MyTextbox.DataBindings.Add(New Binding("Text", Me._listOfItems, "SelectedItem.Comment", True, DataSourceUpdateMode.OnPropertyChanged))
Me.MyListBox.DataSource = Me._listOfItems
this does not work.这不起作用。 But when I bind to SelectedValue instead of SelectedItem it works perfectly.
但是当我绑定到 SelectedValue 而不是 SelectedItem 时,它完美地工作。
The _listOfItems
is declared as this: _listOfItems
声明如下:
Dim _listOfItems As List(Of MyItem) = New List(Of MyItem)()
Where MyItem
is this: MyItem
在哪里:
public class MyItem
{
public string SelectionName { get; set; }
public int Id { get; set; }
public string Comment { get; set; }
}
I tried overriding the ToString()
in MyItem
so that it would use that.我尝试覆盖
MyItem
的ToString()
以便它可以使用它。 But that doesn't work either.但这也行不通。
Anybody care to give it a try?有人愿意试一试吗?
One of the easiest way, I guess, would be to use a BindingSource
, setting it as the ListBox.DataSource
property to your BindingSource
on design.其中一个最简单的方法,我想,会使用
BindingSource
,将其设置为ListBox.DataSource
属性为您BindingSource
设计。
BindingSource
on your form;BindingSource
;ListBox.DataSource
property to your BindingSource
;ListBox.DataSource
属性设置为您的BindingSource
;ValueMember
and DisplayMember
properties just like you're actually doing;ValueMember
和DisplayMember
属性;DataBinding
for your TextBox
control, and use your BindingSource
as the source, using your MyItem.Comment
property;TextBox
控件创建DataBinding
,并使用您的BindingSource
作为源,使用您的MyItem.Comment
属性;List(Of MyItem)
to your Binding.DataSource
property;List(Of MyItem)
分配给您的Binding.DataSource
属性;CurrencyManager.CurrentItem
's Comment property, that is, the currently ListBox.SelectedItem
.CurrencyManager.CurrentItem
的 Comment 属性,即当前的ListBox.SelectedItem
。 Indeed, you would perhaps need to implement the INotifyPropertyChanged
interface to make it work properly.实际上,您可能需要实现
INotifyPropertyChanged
接口才能使其正常工作。
But if this all work perfect with the SelectValue, why don't you just use it?但是,如果这一切都与 SelectValue 完美配合,为什么不直接使用它呢?
The code below shows how I am doing it.下面的代码显示了我是如何做的。 I first set my ListBox DataSource to a class with a collection of BindingList.
我首先将 ListBox DataSource 设置为具有 BindingList 集合的类。 The class implements IBindingList.
该类实现 IBindingList。 I have two TextBoxes I want to bind the SelectedItem to.
我有两个要绑定 SelectedItem 的 TextBox。 The code below is how I am doing it:
下面的代码是我的做法:
lbControl.DataSource = SharepointTestBusinessLayer.Control.ListAll();
lbControl.DisplayMember = "ControlName";
lbControl.SelectedIndex = 0;
scTextBoxControlID.DataBindings.Add("Text", this.lbControl.DataSource, "ControlID");
scTextBoxControlName.DataBindings.Add("Text", this.lbControl.DataSource, "ControlName");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.