Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Story:

I have a ListBox where I add custom objects, when the user clicks these custom objects in that ListBox some WPF Controls are generated in a stackpanel, these WPF Controls are properties of the custom objects. Below you may see the class.

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


I want to know what PropertyName the control the user types into has.

What I have tried:

Private Sub ValuePanel_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles ValuePanel.PreviewKeyDown
    Dim myElement As ComboBox = e.OriginalSource.TemplatedParent

    Dim myVar = GetValue(myElement).GetType().GetProperty("PropertyName")


End Sub


However myElement is not a DependencyObject and cannot be cast to one, I don't know how else to get the PropertyName or the VariableName.
Posted
Updated 7-Jan-22 2:14am
Comments
Ralf Meier 13-Jan-22 9:34am    
I suppose that you look at it the wrong way round ...
If you have a UserControl which has several other Controls in it - why you don't give the UserControl new Properties which work with the Properties from the Controls inside which could by changed by the User.
I suggest you should be more specific ...

1 solution

You don't. The controls have no idea they are a member of your VariableClass class or even a property of a class.

The GetProperty method returns a property of the control itself, not a property of it's parent.

I have no idea what you're doing with this VariableClass, so about the only thing I could suggest is making a UserControl[^] out of your controls.
 
Share this answer
 
Comments
Steve Krozer 7-Jan-22 17:42pm    
I ended up adding a tag to the controls I create with the name of the property, seems to work just fine that way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900