WPF and Powershell - Handling events
Late to the party (by over 4 years). Specifically addressing jpierson's comment.
But in case anyone finds this post, as I did, via Googling PowerShell WPF event handling, wanting to obtain the Sender control (sender) & Event Args (e), here's how...
C# version (non-specific template)
private void Handler(object sender, SomeEventArgs e){ //do something with sender and/or e...}
PowerShell version
$WPFControl.Add_Handler({ $sender = $args[0] $e = $args[1] #do something with sender and/or e...})
And onto a specific MouseWheelHandler event handler
C# version (where the MouseWheelHandler event has been bound to a control)
private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e){ ScrollViewer scv = (ScrollViewer)sender; //do something with sender and/or e...}
PowerShell version
$ScrollViewer.Add_PreviewMouseWheel({ $sender = [System.Windows.Controls.ScrollViewer]$args[0] $e = [System.Windows.Input.MouseWheelEventArgs]$args[1] #do something with sender and/or e...})
In PowerShell, to get the types of sender and event args, use the following
$ScrollViewer.Add_PreviewMouseWheel({ Write-Host $args[0] Write-Host $args[1]})
which (in the above example) will give you...
System.Windows.Controls.ScrollViewerSystem.Windows.Input.MouseWheelEventArgs
Considering WPF and PowerShell, have a look at WPF Linkcollection for PowerShell from Bernd. You will find many interesting links that will help you.
Considering your problem, just use pattern
$control.Add_<someevent>({ what to do })
For example, someevent
could be Click
for a Button:
$button.Add_Click({ $global:clicked = $true })
You pass in a scriptblock that handles the event.
ok, it's a long shot because this post is from 2015, but i tried and it's not working for me.It doesn't rise an error but i don't have access to sender properties
Get-Variable -Name Ctrl_* -ValueOnly | Where-Object {$_.Name -match '^btn_[^_]+_Nav_(Previous|Next)$'} | ForEach-Object { $_.Add_Click({ $sender = [System.Windows.Controls.Button]$args[0] $sender.Name })}
I try to make a generic navigation system for an hmi
Best Regards,Richard