首页 > 解决方案 > 处理运行时添加的控件的事件

问题描述

我在运行时在 WPF 应用程序中添加控件,这些都是不同类型的控件,因此作为UIElement. 然后我获得了type控制权并可以使用SetValue. 由于我有很多控件类型来实现我现在尝试做的是向每个控件添加一个事件,但似乎只能使用它来执行此操作Addhandler,这需要每个控件的大量额外代码,如下所示。我正在寻找一种解决方案,允许我UIElement使用 Cast 将错误处理程序添加到没有事先转换的情况下,我不确定这是否可能?

 Select Case Control.GetType()
                Case GetType(ExtendedTextBox)
                    Dim newControl As ExtendedTextBox = TryCast(Control, ExtendedTextBox)
                    'Dim newCtl As UIElement = TryCast(Control, ExtendedTextBox)
                    If newControl IsNot Nothing Then
                        newControl.SetValue(ExtendedTextBox.HorizontalAlignmentProperty, HorizontalAlignment.Left)
                        newControl.SetValue(ExtendedControlProperties.HighlightTextOnFocusProperty, True)
                        newControl.SetValue(ExtendedControlProperties.MandatoryProperty, True)
                        AddHandler newControl.HasErrors, AddressOf UpdateErrorStatus
                    End If

标签: wpfvb.netcontrols

解决方案


我错误地在验证前加上“客户”。正如在继承控制中导致问题一样,一旦删除它就可以按预期工作。


推荐阅读