I'm trying to build a WPF gui where i have two radiobutton listboxes. A button loads the radio buttons:
function mybuttonclick {
$List = get-content "list.txt"
foreach ($item in $list) {
$tmpradio = New-Object System.Windows.Controls.RadioButton
$tmpradio.content = "$item"
$tmpradio.groupname = "MyList"
Register-ObjectEvent -InputObject $tmpradio -EventName Checked -Action { write-host "test"}
$MyListbox.AddChild($tmpradio)
}
}
However i'm getting nothing back, is it a scope issue where the object gets deleted after exiting from the function?
Is there a solution to make so i can pick an action or another wheter the groupname is "MyList" or for example "MyOtherList"
CodePudding user response:
Looks like i will once more answer my own question..
It honestly is as simple as i tought it would... Jim Moyle explained it, i adapted to my situation..
function mybuttonclick {
$List = get-content "list.txt"
foreach ($item in $list) {
$tmpradio = New-Object System.Windows.Controls.RadioButton
$tmpradio.content = "$item"
$tmpradio.groupname = "MyList"
[System.Windows.RoutedEventHandler]$checkhandler = {write-host 'Test'}
$tmpradio.AddHandler([System.Windows.Controls.RadioButton]::CheckedEvent, $checkhandler)
$MyListbox.AddChild($tmpradio)
}
}
To make it simple i just create my radio button as usual, then i create a handler scriptblock, which is just code executed on x event triggered.
At last i add that handler to my radiobutton on the CheckedEvent.
I could find the event name by opening up a debugger, type down this:
[System.Windows.Controls.RadioButton]::
and then start tabbing for autocompletion