Sunday 14 October 2018

Click Event on Treeview

Sometimes PowerShell and WPF can be tricky to achieve what you want. Treeview controls is not making things any easier. Just for a simple click, you need to go through a lot of trouble.

Before continuing reading, let's introduce some variables:
$treeviewItem is a TreeViewItem controls ([Windows.Controls.TreeViewItem])
$TreeView is the TreeView controls that holds several TreeViewItem.
If you want an example of a TreeView, you can check out this link.

How about using Add_MouseDown({}) ?

You have the MouseDown() event, but it doesn't work pretty much like how we want it. Looks like some issues with WPF they say (on MSDN).  They instead propose to use the "Preview ..." event version. So let us forget about this option! Well, I had no complaints, so I just used the method they proposed.

How do I use those "Preview" thing methods?

When you use the method on your TreeView, just add preview right before like this:

You can see that when you click inside your control, there is always an event fired even if it wasn't a node that you clicked. That's the normal behaviour of the method. However, that's not likely what we want. We just need an event to be fired when a node is clicked.

What should be done to make it works?

Instead of the TreeView itself, you have to assign the method to each TreeViewItem Control. Like this:
  Write-Host "Preview Mouse down " $_.OriginalSource

If you haven't done any graphical customization inside your XAML [used a DataTemplate or something else], this should work. Otherwise, you would say: "What the heck is this dude? It doesn't even work! 😠"

Don't worry! To make it works, you need to collect the right type of Data, which is a [Windows.Controls.TreeViewItem]

By default, This:

  Write-Host "Preview Mouse down" $_.OriginalSource

Equals to this:
 [System.Windows.RoutedEventArgs]$e = $args[1]
 Write-Host "Preview Mouse down " $e.OriginalSource

It returns the source of the event, not the TreeviewItem itself. In some ways, you can directly use this solution depending on what you want to achieve. 

But it's better to say to our script: "Hey give me the right stuff!"
For that, we need to tell Powershell what kind of control he should be waiting for.
Basically, we need to say: "Give me the treeviewItem related to the control I clicked."

To achieve this, you need to do some little modification:

  [System.Windows.Controls.TreeViewItem]$sender = $args[0]
  [System.Windows.RoutedEventArgs]$e = $args[1]  
  Write-Host "Node clicked: " $sender.Header

This way, we can get the "sender" control and we can manipulate it the way we want.

So this is it! You shouldn't have any trouble with your Treeview now.

Want to go further?

If you want to enable right or left click only, just replace the method with:
Add_PreviewMouseLeftButtonDown({}) for left click
Add_PreviewMouseRightButtonDown({}) for right click

Next post will be some drag and drop event with WPF/PowerShell.
Thanks for reading! That's all folks! 😉


0 commentaires:

Post a Comment

Popular Posts

Join us on Facebook


Contact Form


Email *

Message *