Winforms,数据绑定,列表框和文本框 [英] Winforms, databinding, Listbox and textbox

查看:113
本文介绍了Winforms,数据绑定,列表框和文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的屏幕上有一个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 设计。


  1. 在你的表单上删除一个 BindingSource

  2. 设置你的 ListBox.DataSource 属性到您的 BindingSource ;

  3. 设置您的 ValueMember DisplayMember 属性就像你实际上一样;

  4. 使你的 DataBinding 为您的 TextBox 控件,并使用y我们的 BindingSource 作为来源,使用您的 MyItem.Comment 属性;

  5. 将您的列表(Of MyItem)``分配给您的 Binding.DataSource`属性;

  6. 您的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.

  1. Drop a BindingSource on your form;
  2. Set your ListBox.DataSource property to your BindingSource;
  3. Set your ValueMember and DisplayMember properties just like you're actually doing;
  4. Make your DataBinding for your TextBox control, and use your BindingSource as the source, using your MyItem.Comment property;
  5. Assign your List(Of MyItem)``to yourBinding.DataSource` property;
  6. Your TextBox should follow the CurrencyManager.CurrentItem's Comment property, that is, the currently ListBox.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆