User avatar
nadjibus
Topic Author
Posts: 32
Joined: 24 Feb 2022, 04:09

Connect to Child Event

03 Apr 2024, 05:42

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

Re: Connect to Child Event

03 Apr 2024, 06:06

Have you tried overriding ConnectEvent as described in the Events Tutorial ?
 
User avatar
nadjibus
Topic Author
Posts: 32
Joined: 24 Feb 2022, 04:09

Re: Connect to Child Event

03 Apr 2024, 20:38

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

Re: Connect to Child Event

04 Apr 2024, 15:35

Yes, please, add a ticket for this. Thank you!
 
User avatar
sfernandez
Site Admin
Posts: 3001
Joined: 22 Dec 2011, 19:20

Re: Connect to Child Event

04 Apr 2024, 17:16

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.
 
User avatar
nadjibus
Topic Author
Posts: 32
Joined: 24 Feb 2022, 04:09

Re: Connect to Child Event

04 Apr 2024, 20:36

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.
 
User avatar
sfernandez
Site Admin
Posts: 3001
Joined: 22 Dec 2011, 19:20

Re: Connect to Child Event

10 Apr 2024, 10:43

We've updated the events tutorial so documentation will be fixed in the next release, thanks for your feedback.

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 9 guests