mardi 16 juin 2020

Powershell + WPF: how to do listBox with checkBoxes in powershell?

I have a powershell script that at some point produces array of strings. Let's say like so:

$fullList = @('test1', 'test2', 'test3', 'test4')

Of course values are generated and I don't know them ahead of time. End user needs to select subset from that array which the script will use later:

$newList = @('test2', 'test3')

For that I created a simple WPF form:

   Add-Type –assemblyName PresentationFramework
    #Form description XML
 $rawXML = @'
<Window x:Name="MySimpleForm" x:Class="MySimpleForm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MySimpleForm"
        mc:Ignorable="d"
        Title="My Simple Form" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="checkBoxList" HorizontalAlignment="Left" Height="100" Margin="0,0,0,0" VerticalAlignment="Top" Width="100" >
        </ListBox>

    </Grid>
</Window>
'@
#this code cleans XML if needed and generates variables from XAML
    [XML]$XAML = $rawXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'

    $reader = (New-Object System.Xml.XmlNodeReader $xaml)
    try {
        $window = [Windows.Markup.XamlReader]::Load( $reader )
    } catch {
        Write-Warning $_.Exception
        throw
    }

    $xaml.SelectNodes("//*[@Name]") | ForEach-Object {
        #"trying item $($_.Name)"
        try {
            Set-Variable -Name "var_$($_.Name)" -Value $window.FindName($_.Name) -ErrorAction Stop
        } catch {
            throw
        }
    }
    Get-Variable var_*

It's still work in progress so no additional controls to have full functionality as described above. It pretty much generates a window with empty ListBox. And variable responsible for that ListBox called $var_checkBoxList.

Now, I can populate that ListBox with items using this:

$var_checkBoxList.ItemsSource = $fullList

The problem is that populated values are not checkboxes and user cannot select more than one at once. And, as I said, I need a subset from it - so list of check boxes would be ideal. If I add this in XML for my ListBox:

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="False" />
                </DataTemplate>
            </ListBox.ItemTemplate>

Then '$var_checkBoxList.ItemsSource = $fullList' stops working, but I can add empty items which turn out to be checkboxes using this: $var_checkBoxList.Items.Add(). However, I couldn't find how to provide content here so that checkboxes wouldn't look empty and have some text to it.

So, any ideas how to dynamically create a list of checkboxes from an array of strings $fullList so that end user could generate a subset by checking appropriate checkboxes, which I can easily read into $newList as just selected values from that control?

Maybe I can use other approach? Maybe I should use a different control? For instance, there is checkBoxList in Windows Forms? Is there something like that in WPF?




Aucun commentaire:

Enregistrer un commentaire