【问题标题】:Get PropertyName of WPF Control获取 WPF 控件的 PropertyName
【发布时间】:2022-01-07 11:45:05
【问题描述】:

我有一个用自定义对象填充的 ListBox,这些对象具有以下属性,如下所示:

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,它不能强制转换为一个。

【问题讨论】:

  • @mm8 我意识到我刚才打错了,我的意思是 ComboBox 而不是 TextBox,无论哪种方式,我都需要获取控件具有的变量名/属性名。所以,这个是类 VariableClass 的属性的控件是在堆栈面板中创建的:Public Property myNameTextBox As New ComboBox,当我单击列表框中的对象然后在这个 ComboBox 中键入时,我需要确定它的 PropertyName 是否为“ myNameTextBox" 与否。我知道这些控件直接链接到所选对象的属性,因为在对象之间单击我注意到值被记住了。
  • 那么你需要把名字存储在某个地方。 ComboBox 不知道它来自哪个属性...
  • @mm8 但是它怎么知道存储它当前对选定对象的值呢?它肯定会绑定到列表框中的对象,因为当我单击其他对象并返回到我在其中编辑过组合框的那个时,它知道,在调试它时,我也看到值发生了变化!
  • @mm8 不,你误解了,正如我在问题中所说的那样。我有一个列表框,我用自定义对象填充,然后当用户单击它们时,它们作为 WPF 控件的属性在stackpanel,这些 WPF 控件绑定到该列表框中的选定对象(我通过 DEBUG 确认了这一点)。
  • @mm8 我没有绑定它,绑定会自动发生,请查看 Function AddPropertiesInStackPanel(myObject As Object) 函数以获取更多信息。我不明白为什么这个问题被否决了,很清楚问题是什么,我提供了代码和代码的故事情节。功能是添加对象的属性是WPF控件,这些WPF控件是直接链接到那些对象的,所以当我在那些WPF控件中改变一个值时,父对象本身也会更新,在某处和IDK那里有一个链接。跨度>

标签: wpf vb.net


【解决方案1】:

引发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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 2011-05-17
    • 2011-12-15
    • 2011-09-18
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多