如何使用 RoutedEventArgs.OriginalSource 属性来标识发起事件的特定 UIElement [英] How can RoutedEventArgs.OriginalSource Property be used to identify the specific UIElement that originated the event
问题描述
我正在尝试使用 OriginalSource 属性来确定生成 PointerReleased 事件的特定 UIElement.根据文档 RoutedEventArgs.OriginalSource 返回发起事件的对象,但我不知道如何使用它来识别 UIElement.我可以使用 OrginalSource.ToString() 来识别 UIElement 的类型,而不是特定的实例.我的方法可能完全错误,如果是这样,那也值得了解.谢谢.
I'm trying to use the OriginalSource property to determine the specific UIElement that generated a PointerReleased event. According to the documentation RoutedEventArgs.OriginalSource returns the object that originated the event but I don't know how to use that to identify the UIElement. I can use OrginalSource.ToString() to identify the type of UIElement but not a specific instance. My approach may be entirely wrong, if so, that would be worth knowing too. Thanks.
推荐答案
如果您有一组潜在的候选人要交叉检查,您可以简单地使用 ==<在原始来源和每个潜在候选人之间进行平等检查/code> 或使用对象的
.Equals()
方法.
If you have set of potential candidates to cross check against, you can simply do the equality check between original source and each potential candidate using ==
or using .Equals()
method of Object.
其次,如果您希望原始来源表明其身份,您可以做的是在 XAML 中的控件实例上设置 x:Name
,然后在代码中您可以访问 Name
属性以验证其身份.
Secondly, in case you want original source to tell it's identity, what you can do is set x:Name
on control instance in XAML and then in code you can access Name
property to validate it's identity.
XAML:
<TextBlock x:Name="myTextBlock"/>
代码:
string sender = (e.OriginalSource as FrameworkElement).Name;
这篇关于如何使用 RoutedEventArgs.OriginalSource 属性来标识发起事件的特定 UIElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!