如何在 PowerShell WPF 中为复选框列表框添加点击事件?

huangapple go评论52阅读模式
英文:

How to add a Click event on a CheckBox Listbox in Powershell WPF?

问题

I'm looking for a way to add a click handler inside a CheckBox Listbox in Powershell WPF.
我正在寻找一种方法,在Powershell WPF中的CheckBox Listbox中添加点击处理程序。

I need to pass the checked items in the listbox to an array variable whenever any checkbox gets checked/clicked.
每当复选框被选中/单击时,我需要将列表框中已选中的项目传递给数组变量。

Is there any click/mouseLeftButtonUp invocation method I could use on the ListBoxItem? I've found isMouseCaptureChanged and PreviewMouseDown, but they only work when I click off the checkbox item (cant check the box perse).
是否有任何我可以在ListBoxItem上使用的点击/鼠标左键释放调用方法?我找到了isMouseCaptureChanged和PreviewMouseDown,但它们只在我单击复选框项之外时起作用(不能选中复选框)。

To add more context:
为了添加更多背景信息:

I'm trying to make a CheckListBox of the C:\Users folder where you can select multiple users and then pass it to a variable to run a profile backup command.
我正在尝试创建C:\Users文件夹的CheckListBox,您可以在其中选择多个用户,然后将其传递给变量以运行配置文件备份命令。

This is the code I have until now:
这是我到目前为止的代码:

This is the WPF of the CheckBox ListBox:
这是CheckBox ListBox的WPF:

And this is what I have as handler by now (not working as intended):
到目前为止,这是我作为处理程序的内容(不按预期工作):

Also tried with PreviewMouseDown:
还尝试使用PreviewMouseDown:

In this case I can check the item, but it only shows where the listbox index is.
在这种情况下,我可以选中项目,但它只显示列表框索引的位置。

What I'm looking for is to select multiple/single items in the CheckBox ListBox and pass it/them to a variable. Is there any handler for the listbox item?
我想要的是在CheckBox ListBox中选择多个/单个项目并将它们传递给变量。是否有适用于ListBox项的处理程序?

EDIT:
编辑:

Found a workaround about the Check ListBox and now I'm using a stackpanel with togglebuttons, I add the users' buttons by creating them via code when the script runs and I added a general handler for them when they are checked:
找到了Check ListBox的解决方法,现在我使用一个具有ToggleButton的StackPanel,当脚本运行时,我通过代码创建它们,并在它们被选中时为它们添加了一个通用处理程序:

$users | ForEach-Object{
$users | ForEach-Object{
$user = $.Split('')[1] # Here I split the domain of the user and take only the username
$newBtn1 = New-Object System.Windows.Controls.Primitives.ToggleButton
$newBtn1.Name = $user
$newBtn1.Content = $

$newBtn1.AddHandler([System.Windows.Controls.Primitives.ToggleButton]::CheckedEvent,$tbhandler)
$newBtn1.Margin = 2.5
$null = $window.sp_Users.Children.Add($newBtn1)
}

And here's the handler for those ToggleButtons:
以下是这些ToggleButton的处理程序:

[Windows.RoutedEventHandler]$tbHandler = {
[Windows.RoutedEventHandler]$tbHandler = {
Write-Host 'Test'
}
Write-Host 'Test'
}

I want that handler to print the button's name or content when is checked so I can pass it to a variable to update it on the GUI.
我希望当选中按钮时,该处理程序会打印按钮的名称或内容,以便我可以将其传递给变量,以在GUI上进行更新。

英文:

I'm looking for a way to add a click handler inside a CheckBox Listbox in Powershell WPF.
I need to pass the checked items in the listbox to an array variable whenever any checkbox gets checked/clicked.
Is there any click/mouseLeftButtonUp invocation method I could use on the ListBoxItem? I've found isMouseCaptureChanged and PreviewMouseDown, but they only work when I click off the checkbox item (cant check the box perse).

To add more context:
I'm trying to make a CheckListBox of the C:\Users folder where you can select multiple users and then pass it to a variable to run a profile backup command.
This is the code I have until now:

This is the WPF of the CheckBox ListBox:

<Grid Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Label Grid.Row="0" Content="Users to backup:" Foreground="CornflowerBlue"/>
    <StackPanel Grid.Row="1">
        <RadioButton Name="rb_AllUsers" Content="All users" IsChecked="True"/>
        <RadioButton Name="rb_Users" Content="Selected users:"/>
    </StackPanel>
    <ListBox Grid.Row="2" Margin="5" Name="lb_Users" ScrollViewer.VerticalScrollBarVisibility="Auto" IsEnabled="False">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding DisplayName}" IsChecked="{Binding bChecked}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

And this is what I have as handler by now (not working as intended):

$window.lb_Users.Add_IsMouseCapturedChanged({
    if($window.lb_Users.SelectedItems){
      Write-Host $window.lb_Users.SelectedItem
    }
})

(note that the Write-Host is only for testing purposes)

Also tried with PreviewMouseDown:

$window.lb_Users.Add_PreviewMouseDown({
  if($window.lb_Users.SelectedItem.bChecked -eq $true){
    Write-Host $window.lb_Users.SelectedItem
  }
})

In this case I can check the item, but it only shows where the listbox index is.

What I'm looking for is to select multiple/single items in the CheckBox ListBox and pass it/them to a variable. Is there any handler for the listbox item?

EDIT:

Found a workaround about the Check ListBox and now I'm using a stackpanel with togglebuttons, I add the users's buttons by creating them via code when the script runs and I added a general handler for them when they are checked:

$users | ForEach-Object{
  $user = $_.Split('\')[1] # Here I split the domain of the user and take only the username
  $newBtn1 = New-Object System.Windows.Controls.Primitives.ToggleButton
  $newBtn1.Name = $user
  $newBtn1.Content = $_
  $newBtn1.AddHandler([System.Windows.Controls.Primitives.ToggleButton]::CheckedEvent,$tbhandler)
  $newBtn1.Margin = 2.5
  $null = $window.sp_Users.Children.Add($newBtn1)
}

And here's the handler for those ToggleButtons:

[Windows.RoutedEventHandler]$tbHandler = {
   Write-Host 'Test'
}

I want that handler to print the button's name or content when is checked so I can pass it to a variable to update it on the GUI.
Any way on how can I do so?

答案1

得分: 1

尝试处理ToggleButton.Checked附加事件,用于ListBox

我认为在Powershell中应该像这样实现:

[System.Windows.RoutedEventHandler]$handler = {
   ...
}
...
$window.lb_Users.AddHandler([System.Windows.Controls.CheckBox]::CheckedEvent, $handler)
英文:

Try to handle the ToggleButton.Checked attached event for the ListBox.

I believe it should be implemented something like this in Powershell:

[System.Windows.RoutedEventHandler]$handler = {
   ...
}
...
$window.lb_Users.AddHandler([System.Windows.Controls.CheckBox]::CheckedEvent, $handler)

huangapple
  • 本文由 发表于 2023年3月3日 23:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定