Home > other >  Get PropertyName of WPF Control
Get PropertyName of WPF Control

Time:01-08

I have a ListBox that I fill with custom objects, these objects have the following properties as seen below:

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

When the user clicks on any of the custom objects, the properties of the custom objects that are WPF controls are pragmatically created in a stackpanel.

I do it like this:

            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

The functions:

 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

I have tried this event that triggers whenever the user types into a ComboBox in the stackpanel.

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

But myElement is not a DependencyProperty and it cannot be cast to one.

CodePudding user response:

The control that raises the PreviewKeyDown doesn't know which propery of your custom object it "came" from. You can however store this information in the Tag property of the control, e.g.:

myNameTextBox.Tag = "myNameLabel"

...and then retrieve it in the event handler using this property:

Private Sub ValuePanel_PreviewKeyDown(sender As Object, e As KeyEventArgs)
    Dim myElement = CType(e.OriginalSource, FrameworkElement)

    Dim propertyName = myElement.Tag.ToString()

End Sub
  •  Tags:  
  • Related