获取 WPF 控件的 PropertyName
Get PropertyName of WPF Control
我有一个用自定义对象填充的列表框,这些对象具有以下属性,如下所示:
Public Class VariableClass
Public Property Content As String
Public Property myNameLabel As New Label
Public Property myNameTextBox As New ComboBox
Public Property myTypeLabel As New Label
Public Property myTypeTextBox As New ComboBox
Public Overrides Function ToString() As String
Return Content.ToString()
End Function
End Class
当用户单击任何自定义对象时,作为 WPF 控件的自定义对象的属性将在堆栈面板中实际创建。
我是这样做的:
If Flowchart.SelectedItem.GetType.Name = "VariableClass" Then
SetLabelProperties(Flowchart.SelectedItem.myNameLabel, "Name:", "25", LabelPanel.Width, HorizontalAlignment.Center)
SetLabelProperties(Flowchart.SelectedItem.myTypeLabel, "Type:", "25", LabelPanel.Width, HorizontalAlignment.Center)
SetComboBoxProperties(Flowchart.SelectedItem.myNameTextBox, Flowchart.SelectedItem.myNameTextBox.Text, "25", ValuePanel.Width, VerticalContentAlignment.Center, True, True)
SetComboBoxProperties(Flowchart.SelectedItem.myTypeTextBox, Flowchart.SelectedItem.myTypeTextBox.Text, "25", ValuePanel.Width, VerticalContentAlignment.Center, True, True)
AddPropertiesInStackPanel(Flowchart.SelectedItem)
End if
函数:
Function SetLabelProperties(myLabel As Label, myName As String, myHeight As String, myWidth As String, myHorizontalAlignment As HorizontalAlignment)
myLabel.Content = myName
myLabel.Height = myHeight
myLabel.Width = myWidth
myLabel.HorizontalContentAlignment = myHorizontalAlignment
End Function
Function SetComboBoxProperties(myComboBox As ComboBox, myName As String, myHeight As String, myWidth As String, myVerticalAlignment As VerticalAlignment, Editable As Boolean, SearchEnabled As Boolean)
myComboBox.IsEditable = Editable
myComboBox.IsTextSearchEnabled = SearchEnabled
myComboBox.Text = myName
myComboBox.Height = myHeight
myComboBox.Width = myWidth
myComboBox.VerticalContentAlignment = myVerticalAlignment
Dim myDouble As Double = 0
Dim myStatic As ResourceKey = SystemParameters.VerticalScrollBarWidthKey
myComboBox.Resources.Add(myStatic, myDouble)
End Function
Function SetTextBoxProperties(myTextBox As TextBox, myName As String, myHeight As String, myWidth As String, myVerticalAlignment As VerticalAlignment)
myTextBox.Text = myName
myTextBox.Height = myHeight
myTextBox.Width = myWidth
myTextBox.VerticalContentAlignment = myVerticalAlignment
End Function
Function AddPropertiesInStackPanel(myObject As Object)
Dim info() As PropertyInfo = myObject.GetType().GetProperties()
For Each item In info
Dim myElementSplit = Split(item.PropertyType.FullName, ".")(UBound(Split(item.PropertyType.FullName, ".")))
If myElementSplit = "Label" Then
LabelPanel.Children.Add(item.GetValue(myObject))
LabelPanel.Children.Add(New Separator With {.Height = 1, .Opacity = 0})
ElseIf myElementSplit = "ComboBox" Then
ValuePanel.Children.Add(item.GetValue(myObject))
ValuePanel.Children.Add(New Separator With {.Height = 1, .Opacity = 0})
ElseIf myElementSplit = "TextBox" Then
ValuePanel.Children.Add(item.GetValue(myObject))
ValuePanel.Children.Add(New Separator With {.Height = 1, .Opacity = 0})
End If
Next
End Function
我已经尝试过这个事件,只要用户在堆栈面板中的 ComboBox 中键入内容就会触发。
Private Sub ValuePanel_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles ValuePanel.PreviewKeyDown
Dim myElement = e.OriginalSource
Dim myVar = GetValue(myElement).GetType().GetProperty("PropertyName")
End Sub
但是 myElement 不是 DependencyProperty,不能转换为一个。
引发 PreviewKeyDown
的控件不知道它“来自”自定义对象的哪个 属性。但是,您可以将此信息存储在控件的 Tag
属性 中,例如:
myNameTextBox.Tag = "myNameLabel"
...然后使用此 属性:
在事件处理程序中检索它
Private Sub ValuePanel_PreviewKeyDown(sender As Object, e As KeyEventArgs)
Dim myElement = CType(e.OriginalSource, FrameworkElement)
Dim propertyName = myElement.Tag.ToString()
End Sub
我有一个用自定义对象填充的列表框,这些对象具有以下属性,如下所示:
Public Class VariableClass
Public Property Content As String
Public Property myNameLabel As New Label
Public Property myNameTextBox As New ComboBox
Public Property myTypeLabel As New Label
Public Property myTypeTextBox As New ComboBox
Public Overrides Function ToString() As String
Return Content.ToString()
End Function
End Class
当用户单击任何自定义对象时,作为 WPF 控件的自定义对象的属性将在堆栈面板中实际创建。
我是这样做的:
If Flowchart.SelectedItem.GetType.Name = "VariableClass" Then
SetLabelProperties(Flowchart.SelectedItem.myNameLabel, "Name:", "25", LabelPanel.Width, HorizontalAlignment.Center)
SetLabelProperties(Flowchart.SelectedItem.myTypeLabel, "Type:", "25", LabelPanel.Width, HorizontalAlignment.Center)
SetComboBoxProperties(Flowchart.SelectedItem.myNameTextBox, Flowchart.SelectedItem.myNameTextBox.Text, "25", ValuePanel.Width, VerticalContentAlignment.Center, True, True)
SetComboBoxProperties(Flowchart.SelectedItem.myTypeTextBox, Flowchart.SelectedItem.myTypeTextBox.Text, "25", ValuePanel.Width, VerticalContentAlignment.Center, True, True)
AddPropertiesInStackPanel(Flowchart.SelectedItem)
End if
函数:
Function SetLabelProperties(myLabel As Label, myName As String, myHeight As String, myWidth As String, myHorizontalAlignment As HorizontalAlignment)
myLabel.Content = myName
myLabel.Height = myHeight
myLabel.Width = myWidth
myLabel.HorizontalContentAlignment = myHorizontalAlignment
End Function
Function SetComboBoxProperties(myComboBox As ComboBox, myName As String, myHeight As String, myWidth As String, myVerticalAlignment As VerticalAlignment, Editable As Boolean, SearchEnabled As Boolean)
myComboBox.IsEditable = Editable
myComboBox.IsTextSearchEnabled = SearchEnabled
myComboBox.Text = myName
myComboBox.Height = myHeight
myComboBox.Width = myWidth
myComboBox.VerticalContentAlignment = myVerticalAlignment
Dim myDouble As Double = 0
Dim myStatic As ResourceKey = SystemParameters.VerticalScrollBarWidthKey
myComboBox.Resources.Add(myStatic, myDouble)
End Function
Function SetTextBoxProperties(myTextBox As TextBox, myName As String, myHeight As String, myWidth As String, myVerticalAlignment As VerticalAlignment)
myTextBox.Text = myName
myTextBox.Height = myHeight
myTextBox.Width = myWidth
myTextBox.VerticalContentAlignment = myVerticalAlignment
End Function
Function AddPropertiesInStackPanel(myObject As Object)
Dim info() As PropertyInfo = myObject.GetType().GetProperties()
For Each item In info
Dim myElementSplit = Split(item.PropertyType.FullName, ".")(UBound(Split(item.PropertyType.FullName, ".")))
If myElementSplit = "Label" Then
LabelPanel.Children.Add(item.GetValue(myObject))
LabelPanel.Children.Add(New Separator With {.Height = 1, .Opacity = 0})
ElseIf myElementSplit = "ComboBox" Then
ValuePanel.Children.Add(item.GetValue(myObject))
ValuePanel.Children.Add(New Separator With {.Height = 1, .Opacity = 0})
ElseIf myElementSplit = "TextBox" Then
ValuePanel.Children.Add(item.GetValue(myObject))
ValuePanel.Children.Add(New Separator With {.Height = 1, .Opacity = 0})
End If
Next
End Function
我已经尝试过这个事件,只要用户在堆栈面板中的 ComboBox 中键入内容就会触发。
Private Sub ValuePanel_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles ValuePanel.PreviewKeyDown
Dim myElement = e.OriginalSource
Dim myVar = GetValue(myElement).GetType().GetProperty("PropertyName")
End Sub
但是 myElement 不是 DependencyProperty,不能转换为一个。
引发 PreviewKeyDown
的控件不知道它“来自”自定义对象的哪个 属性。但是,您可以将此信息存储在控件的 Tag
属性 中,例如:
myNameTextBox.Tag = "myNameLabel"
...然后使用此 属性:
在事件处理程序中检索它Private Sub ValuePanel_PreviewKeyDown(sender As Object, e As KeyEventArgs)
Dim myElement = CType(e.OriginalSource, FrameworkElement)
Dim propertyName = myElement.Tag.ToString()
End Sub