Page 1 of 2
Get element name in event handler
Posted: 17 Apr 2015, 02:02
by Rick
I'm trying to get the element that raised the event in the event handler.
void LuaNoesis::Element_OnRoutedEventArgs(BaseComponent* sender, const RoutedEventArgs& e)
{
NsString elementName = sender->ToString();
}
But the above code returns the type of the sender. How can I get the senders name? There is no GetName() function on it like I would have suspected.
Re: Get element name in event handler
Posted: 17 Apr 2015, 02:12
by jsantos
The name that you set using x:Name in the XAML is stored in the Name property of the
FrameworkElement class
/// Gets or sets the identifying name of the element. The name provides a reference so that
/// code-behind, such as event handler code, can refer to a markup element after it is
/// constructed during processing by a XAML processor.
//@{
const NsChar* GetName() const;
void SetName(const NsChar* name);
//@}
Re: Get element name in event handler
Posted: 17 Apr 2015, 02:15
by jsantos
Although, I don't know if WPF in that case also returns the x:Name when doing System.Object.ToString
We have to verify it and change the behavior if it is not the same.
Re: Get element name in event handler
Posted: 17 Apr 2015, 02:38
by Rick
Curious as to why I don't see BaseComponent in the class list? That's what we have in the sender parameter of events.
Re: Get element name in event handler
Posted: 17 Apr 2015, 18:24
by jsantos
Well, the class hierarchy is intended to be API agnostic (valid for C#, C++). So we excluded specific platform details like root classes, that are different on each platform.
Re: Get element name in event handler
Posted: 17 Apr 2015, 18:29
by Rick
OK
So is the below a takeaway that you are looking at? I don't seem to have a way to know the control name (the sender) of the event.
Although, I don't know if WPF in that case also returns the x:Name when doing System.Object.ToString
We have to verify it and change the behavior if it is not the same.
Any workarounds that you can think of for this?
Re: Get element name in event handler
Posted: 17 Apr 2015, 18:33
by jsantos
Hmm... yes, as said in my previous post, you must use FrameworkElement::GetName()
have you tried it?
Re: Get element name in event handler
Posted: 17 Apr 2015, 18:35
by jsantos
void LuaNoesis::Element_OnRoutedEventArgs(BaseComponent* sender, const RoutedEventArgs& e)
{
NsString elementName;
FrameworkElement* element = NsDynamicCast<FrameworkElement*>(sender);
if (element != 0)
{
elementName = element->GetName();
}
}
Re: Get element name in event handler
Posted: 17 Apr 2015, 18:38
by Rick
Oh, I'm sorry I didn't realize you wanted me to cast the sender object (use NsDynamicCast which I haven't ran into before). I was wondering how I was supposed to get this FrameworkElement object but understand now. I'll give this a try tonight. Thanks.
Re: Get element name in event handler
Posted: 17 Apr 2015, 18:42
by jsantos
Yes, I should have written the source code in my first post.
By the way, I have confirmed that in WPF, ToString() does not use the x:Name. So, our implementation is correct.