Winforms,数据绑定,列表框和文本框 [英] Winforms, databinding, Listbox and textbox
问题描述
我的屏幕上有一个ListBox( MyListBox
)和一个Textbox( MyTextBox
)。 >
列表框中填有一个列表(Of T),这些都是自定义项。
现在我试着做这个:
ListBox的数据源是List(Of T)。
希望将文本框更新为ListBox中所选项目的特定属性。
在代码中,这意味着:
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
这不行。但是当我绑定到SelectedValue而不是SelectedItem它是完美的。
_listOfItems
被声明为: p>
Dim _listOfItems As List(Of MyItem)=新列表(MyItem)()
其中 MyItem
是这样的:
public class MyItem
{
public string SelectionName {get;组; }
public int Id {get;组; }
public string Comment {get;组;
}
我尝试覆盖 ToString() code>在
MyItem
中,以便使用它。但是这也不行。
任何人都要试一试?
谢谢! p>
-Snakiej
我想,最简单的方法之一就是要使用 BindingSource
,将其设置为 ListBox.DataSource
属性到您的 BindingSource
设计。
- 在你的表单上删除一个
BindingSource
- 设置你的
ListBox.DataSource
属性到您的BindingSource
; - 设置您的
ValueMember
和DisplayMember
属性就像你实际上一样; - 使你的
DataBinding
为您的TextBox
控件,并使用y我们的BindingSource
作为来源,使用您的MyItem.Comment
属性; - 将您的
列表(Of MyItem)``分配给您的
Binding.DataSource`属性; - 您的TextBox应遵循
CurrencyManager.CurrentItem
的注释属性,即目前的ListBox.SelectedItem
。
的确,您可能需要实现 INotifyPropertyChanged
界面使其正常工作。
但如果这一切都使用SelectValue完美工作,为什么不用它?
I have a ListBox (MyListBox
) on my screen, and a Textbox (MyTextBox
).
The ListBox is filled with a List(Of T), which are all custom items.
Now I try to do this:
The ListBox' datasource is the 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.
The _listOfItems
is declared as this:
Dim _listOfItems As List(Of MyItem) = New List(Of MyItem)()
Where MyItem
is this:
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. But that doesn't work either.
Anybody care to give it a try?
Thanks!
-Snakiej
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.
- Drop a
BindingSource
on your form; - Set your
ListBox.DataSource
property to yourBindingSource
; - Set your
ValueMember
andDisplayMember
properties just like you're actually doing; - Make your
DataBinding
for yourTextBox
control, and use yourBindingSource
as the source, using yourMyItem.Comment
property; - Assign your
List(Of MyItem)``to your
Binding.DataSource` property; - Your TextBox should follow the
CurrencyManager.CurrentItem
's Comment property, that is, the currentlyListBox.SelectedItem
.
Indeed, you would perhaps need to implement the INotifyPropertyChanged
interface to make it work properly.
But if this all work perfect with the SelectValue, why don't you just use it?
这篇关于Winforms,数据绑定,列表框和文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!