Get element name in event handler
I'm trying to get the element that raised the event in the event handler.
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.
Code: Select all
void LuaNoesis::Element_OnRoutedEventArgs(BaseComponent* sender, const RoutedEventArgs& e)
{
NsString elementName = sender->ToString();
}
Re: Get element name in event handler
The name that you set using x:Name in the XAML is stored in the Name property of the FrameworkElement class
Code: Select all
/// 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
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.
We have to verify it and change the behavior if it is not the same.
Re: Get element name in event handler
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
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
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.
Any workarounds that you can think of for this?
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.
Code: Select all
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
Hmm... yes, as said in my previous post, you must use FrameworkElement::GetName()
have you tried it?
have you tried it?
Re: Get element name in event handler
Code: Select all
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
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
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.
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: Google [Bot], Semrush [Bot] and 5 guests