PS C:\>

Unleash your script

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.

Use MahApps.metro built-in dialogs

Apply beautiful modal dialog into your project.

How to apply a theme on your GUI?

Some tips on how to apply a theme on your GUI ...

Showing posts with label TreeView. Show all posts
Showing posts with label TreeView. Show all posts

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:
$TreeView.Add_PreviewMouseDown({})

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:
$treeViewItem.Add_PreviewMouseDown({
  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:

$treeViewItem.Add_PreviewMouseDown({
  Write-Host "Preview Mouse down" $_.OriginalSource
})

Equals to this:
$treeViewItem.Add_PreviewMouseDown({
 [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:


$
treeViewItem.Add_PreviewMouseDown({
  [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! 😉


Share:

Monday, 16 April 2018

PowerShell WPF - Customize TreeView Icon



       I have seen many people struggle with this so I decided to write something about TreeView in PowerShell. 

The script in this post is based on the project available here (written in C#). I simply rewrote it in PowerShell and added my personal touch.  He displayed a drive image if the TreevieItem was a drive and a folder image otherwise. I extended the principle a little; you can specify directly in your script which kind of image you want to use in which case.



Share:

Popular Posts

Join us on Facebook

STATS

Contact Form

Name

Email *

Message *

image
Hello,

Thank you for visiting my blog and reading this far.

I'm mainly focusing on scripting, software development and modern workpalce experiences with Microsoft Technology. I created this blog for the main purpose of sharing my knowledge. Hope you will appreciate my works and will help you in yours. You are friendly invited to leave any kind of feedback in the comment sections of my posts.

Please note that all posts on this blog are my own opinions and are provided "as is", without warranty of any kind.