How can RoutedEventArgs.OriginalSource Property be used to identify the specific UIElement that originated the event

我与影子孤独终老i 提交于 2019-12-12 03:23:17

问题


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.


回答1:


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.

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"/>

Code:

string sender = (e.OriginalSource as FrameworkElement).Name;


来源:https://stackoverflow.com/questions/26937434/how-can-routedeventargs-originalsource-property-be-used-to-identify-the-specific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!