wpf - 处理运行时添加的控件的事件
问题描述
我在运行时在 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
解决方案
我错误地在验证前加上“客户”。正如在继承控制中导致问题一样,一旦删除它就可以按预期工作。
推荐阅读
- python - 如何在我的 python 程序中使用输入
- python - 用相似的字符串Python在两列上加入两个数据框
- c++ - 标头中的模板化 C++ 函数如何发送到链接器?
- c++ - 在另一个类中的另一个 << 重载中使用重载的 << 运算符
- laravel - Laravel Eloquent 结果未进入查找所有查询
- javascript - REST 服务从 curl 工作,而不是从应用程序工作
- c# - Asp.Net Core 动态更新枚举项目(添加项目、删除项目等)
- c++ - GCP C++ SDK - 设置 HTTP 代理和禁用 SSL 证书验证
- python-3.x - 如何让我的 python 子文件通过导入看到外部文件?
- reactjs - react:手动触发输入组件合成变化事件