3

Can anyone point me in the right direction for some documentation about handling WPF UI events in Powershell?

I want to know how to, for example, call a function when a CheckBox or Radio Button is changed.

Cheers!

Ben

3 Answers 3

11

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.ScrollViewer
System.Windows.Input.MouseWheelEventArgs
Sign up to request clarification or add additional context in comments.

1 Comment

I've serached for a 3 weeks on SO and Microsoft forums, and no one is able to give me an answer on simple quesion, how to work with events in PowerShell, and as I assumed it's really easy. Thx a lot, you never late to the party ;)
7

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.

1 Comment

How do you get at the event args and/or sender arguments.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.