Rick
Topic Author
Posts: 50
Joined: 26 Nov 2013, 15:35

Get element name in event handler

17 Apr 2015, 02:02

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.
 
User avatar
jsantos
Site Admin
Posts: 3919
Joined: 20 Jan 2012, 17:18
Contact:

Re: Get element name in event handler

17 Apr 2015, 02:12

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);
 //@}
 
User avatar
jsantos
Site Admin
Posts: 3919
Joined: 20 Jan 2012, 17:18
Contact:

Re: Get element name in event handler

17 Apr 2015, 02:15

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.
 
Rick
Topic Author
Posts: 50
Joined: 26 Nov 2013, 15:35

Re: Get element name in event handler

17 Apr 2015, 02:38

Curious as to why I don't see BaseComponent in the class list? That's what we have in the sender parameter of events.
 
User avatar
jsantos
Site Admin
Posts: 3919
Joined: 20 Jan 2012, 17:18
Contact:

Re: Get element name in event handler

17 Apr 2015, 18:24

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.
 
Rick
Topic Author
Posts: 50
Joined: 26 Nov 2013, 15:35

Re: Get element name in event handler

17 Apr 2015, 18:29

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?
 
User avatar
jsantos
Site Admin
Posts: 3919
Joined: 20 Jan 2012, 17:18
Contact:

Re: Get element name in event handler

17 Apr 2015, 18:33

Hmm... yes, as said in my previous post, you must use FrameworkElement::GetName()

have you tried it?
 
User avatar
jsantos
Site Admin
Posts: 3919
Joined: 20 Jan 2012, 17:18
Contact:

Re: Get element name in event handler

17 Apr 2015, 18:35

void LuaNoesis::Element_OnRoutedEventArgs(BaseComponent* sender, const RoutedEventArgs& e)
{
   NsString elementName;

   FrameworkElement* element = NsDynamicCast<FrameworkElement*>(sender);
   if (element != 0)
   {
      elementName = element->GetName();
   }
}
 
Rick
Topic Author
Posts: 50
Joined: 26 Nov 2013, 15:35

Re: Get element name in event handler

17 Apr 2015, 18:38

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.
 
User avatar
jsantos
Site Admin
Posts: 3919
Joined: 20 Jan 2012, 17:18
Contact:

Re: Get element name in event handler

17 Apr 2015, 18:42

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.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Semrush [Bot] and 13 guests