Page 1 of 1

Connect to Child Event

Posted: 03 Apr 2024, 05:42
by nadjibus
Hi! I'm trying to create a TreeView and listen to its TreeViewItems Expand event. In WPF, it's as simple as:
<TreeView TreeViewItem.Expanded="OnItemExpanded" />
How do we connect to this event via code-behind in Noesis?

Re: Connect to Child Event

Posted: 03 Apr 2024, 06:06
by jsantos
Have you tried overriding ConnectEvent as described in the Events Tutorial ?

Re: Connect to Child Event

Posted: 03 Apr 2024, 20:38
by nadjibus
Yes that's the first thing I tried, but the method gets invoked when the UserControl is initialized, with param (object source) pointing to the TreeView itself. But the event is for the future child items (TreeViewItem) once they're added.

The workaround is to register to the event by code
MyTreeView.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(OnTreeViewExpanded));
I can add a ticket if you want so ConnectEvent also gets invoked for children events when they're added?

Re: Connect to Child Event

Posted: 04 Apr 2024, 15:35
by jsantos
Yes, please, add a ticket for this. Thank you!

Re: Connect to Child Event

Posted: 04 Apr 2024, 17:16
by sfernandez
Hello, I want to mention that we already define a macro in C++ for attached events to be used in ConnectEvent:
bool ConnectEvent(BaseComponent* source, const char* event, const char* handler) override
{
  NS_CONNECT_ATTACHED_EVENT(TreeViewItem, Expanded, OnTreeViewExpanded);
  return false;
}
And for C#, the code you posted is the expected behavior (the routed event will bubble up the tree from the TreeViewItem until it reaches the TreeView, so it will be processed for any TreeViewItem):
protected override bool ConnectEvent(object source, string eventName, string handlerName)
{
  if (eventName == "Expanded" && handlerName == "OnTreeViewExpanded")
  {
    ((TreeView)source).AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(OnTreeViewExpanded));
  }
}
We need to explain this in the tutorial too.

Re: Connect to Child Event

Posted: 04 Apr 2024, 20:36
by nadjibus
Yes this is how I ended doing it. Thank you!

I don't think a ticket is necessary anymore, but yes explaining it in the tutorial would be great for future users.

Re: Connect to Child Event

Posted: 10 Apr 2024, 10:43
by sfernandez
We've updated the events tutorial so documentation will be fixed in the next release, thanks for your feedback.