包含更多信息的树视图节点
Treeview node with more information
我想在树视图节点中自定义信息,所以我做了这个class
Public Class TreeViewItem
Inherits TreeNode
Private _text As String
Private _id As String
Private _Data As String
Sub New(id As String, name As String, data As String)
MyBase.New()
_text = Name
_id = id
_Data = data
End Sub
Public Shadows Property Text As String
Get
If Not String.IsNullOrEmpty(_Data) Then
Return String.Format("{0} -> {1}", _text, _Data)
Else
Return _text
End If
End Get
Set(value As String)
_text = Name
End Set
End Property
Public Property ID As String
Get
Return _id
End Get
Set(value As String)
_id = value
End Set
End Property
Public Property Data As String
Get
Return _Data
End Get
Set(value As String)
_Data = value
End Set
End Property
End Class
但是当我像这样添加一个节点时
tv.Nodes.Add(New TreeViewItem(1, "hello", "hi"))
节点的文本是空的,请问为什么没有渲染?
您几乎必须使用基础 class 的文本 属性,因此请尝试改用重载,以便您可以 设置 值:
Public Overloads Property Text As String
Get
Return MyBase.Text
End Get
Set(value As String)
_text = value
If Not String.IsNullOrEmpty(_Data) Then
MyBase.Text = String.Format("{0} -> {1}", value, _Data)
Else
MyBase.Text = value
End If
End Set
End Property
这会将您的构造函数更改为:
Sub New(id As String, name As String, data As String)
MyBase.New()
_id = id
_Data = data
_text = name
Me.Text = _text
End Sub
并更新您的数据属性:
Public Property Data As String
Get
Return _Data
End Get
Set(value As String)
_Data = value
Me.Text = _text
End Set
End Property
我想在树视图节点中自定义信息,所以我做了这个class
Public Class TreeViewItem
Inherits TreeNode
Private _text As String
Private _id As String
Private _Data As String
Sub New(id As String, name As String, data As String)
MyBase.New()
_text = Name
_id = id
_Data = data
End Sub
Public Shadows Property Text As String
Get
If Not String.IsNullOrEmpty(_Data) Then
Return String.Format("{0} -> {1}", _text, _Data)
Else
Return _text
End If
End Get
Set(value As String)
_text = Name
End Set
End Property
Public Property ID As String
Get
Return _id
End Get
Set(value As String)
_id = value
End Set
End Property
Public Property Data As String
Get
Return _Data
End Get
Set(value As String)
_Data = value
End Set
End Property
End Class
但是当我像这样添加一个节点时
tv.Nodes.Add(New TreeViewItem(1, "hello", "hi"))
节点的文本是空的,请问为什么没有渲染?
您几乎必须使用基础 class 的文本 属性,因此请尝试改用重载,以便您可以 设置 值:
Public Overloads Property Text As String
Get
Return MyBase.Text
End Get
Set(value As String)
_text = value
If Not String.IsNullOrEmpty(_Data) Then
MyBase.Text = String.Format("{0} -> {1}", value, _Data)
Else
MyBase.Text = value
End If
End Set
End Property
这会将您的构造函数更改为:
Sub New(id As String, name As String, data As String)
MyBase.New()
_id = id
_Data = data
_text = name
Me.Text = _text
End Sub
并更新您的数据属性:
Public Property Data As String
Get
Return _Data
End Get
Set(value As String)
_Data = value
Me.Text = _text
End Set
End Property