我在屏幕上有一个列表框(MyListBox
)和一个文本框(MyTextBox
)。
列表框填充了一个自定义项的泛型列表(List(Of T))。
现在我尝试做到这一点:
列表框的数据源是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
声明如下:
Dim _listOfItems As List(Of MyItem) = New List(Of MyItem)()
当 MyItem
是以下内容时:
public class MyItem
{
public string SelectionName { get; set; }
public int Id { get; set; }
public string Comment { get; set; }
}
我尝试在MyItem
中覆盖ToString()
方法,以便它可以使用该方法。但这也不起作用。
有人愿意试试吗?