将 VB.NET label.text 绑定到对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19083165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding a VB.NET label.text to an object property
提问by Eric Snyder
I want to have a label in a form whose text value changes depending upon the value of an instance of a class. It looks like I can bind the text value of the label to an object dataSource. When I try this it does not seem to work.
我想要一个形式的标签,其文本值会根据类的实例的值而变化。看起来我可以将标签的文本值绑定到对象数据源。当我尝试这个时,它似乎不起作用。
Me.Label4.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.ItemInfoBindingSource, "ItemNumber", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
My itemInfoBindingSource:
我的 itemInfoBindingSource:
Me.ItemInfoBindingSource.DataSource = GetType(CFP.ItemInfo)
and the class definition:
和类定义:
Public Class ItemInfo
Public Property ItemNumber As String = "rename"
Public Property Description As String
Public Property FileLocation As String
Public Property CompileHistory As List(Of CompileHistory)
End Class
I think what I have done is to bind to a class, not an instance of a class. Thinking about it, what I really want to do is bind an instance of a class to a label... How? Is this possible?
我认为我所做的是绑定到一个类,而不是一个类的实例。想了想,我真正想做的是将一个类的实例绑定到一个标签上......如何?这可能吗?
回答by kvermeer
Yes, this is possible, but you need to raise an event to let the label know that the property has changed. If you were using a type like a BindingList, this would be done automatically, but you're trying to bind to a Stringwhich doesn't raise PropertyChanged events.
是的,这是可能的,但是您需要引发一个事件,让标签知道该属性已更改。如果您使用的是 BindingList 之类的类型,这将自动完成,但您正在尝试绑定到String不引发 PropertyChanged events 的类型。
To add the event to your class:
要将事件添加到您的班级:
- Change your class definition to implement INotifyPropertyChanged
- Add the corresponding PropertyChanged event
- Change the auto-implemented property to an expanded property and raise the event.
- 更改您的类定义以实现 INotifyPropertyChanged
- 添加对应的 PropertyChanged 事件
- 将自动实现的属性更改为扩展属性并引发事件。
Here's the result of these changes for just the ItemNumber property in your class:
以下是您类中仅 ItemNumber 属性的这些更改的结果:
Public Class ItemInfo
Implements System.ComponentModel.INotifyPropertyChanged
Private _itemNumber As String = "rename"
Public Property ItemNumber As String
Get
Return _itemNumber
End Get
Set(value As String)
_itemNumber = value
RaiseEvent PropertyChanged(Me,
New System.ComponentModel.PropertyChangedEventArgs("ItemNumber"))
End Set
End Property
Public Event PropertyChanged(sender As Object,
e As System.ComponentModel.PropertyChangedEventArgs) _
Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
I added a text box and label to a form, added the data binding in the Form.Loadevent, added a field called ItemInfoBindingSource of type ItemInfo, and updated the ItemNumber in the TextBox.TextChangedevent.
我在表单中添加了一个文本框和标签,在Form.Load事件中添加了数据绑定,添加了一个名为 ItemInfoBindingSource 的 ItemInfo 类型的字段,并更新了TextBox.TextChanged事件中的 ItemNumber 。
Private ItemInfoBindingSource As New ItemInfo
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.DataBindings.Add("Text", Me.ItemInfoBindingSource, "ItemNumber")
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
ItemInfoBindingSource.ItemNumber = TextBox1.Text
End Sub
Now, when you type in the text box, ItemNumber.Set is called, and raises an event to let anything listening know that it's been changed. The label is listening, and it updates its Text property so that you can see the new value.
现在,当您在文本框中键入时,将调用 ItemNumber.Set,并引发一个事件,让任何正在侦听的内容都知道它已被更改。标签正在侦听,并更新其 Text 属性,以便您可以看到新值。

