PowerShell and WPF: Checkboxes

Continuing on with my series on PowerShell and WPF, I am going to talk about using checkboxes in a form.

Checkboxes are a great way to allow a user to select a specific item or items prior to moving on with an action.

An example of this is below:

#Build the GUI
[xml]$xaml = @"
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
    SizeToContent = "WidthAndHeight" ShowInTaskbar = "True" Background = "lightgray"> 
    <StackPanel > 
        <CheckBox x:Name="Item1" Content = 'Item1'/>
        <CheckBox x:Name="Item2" Content = 'Item2'/>
        <CheckBox x:Name="Item3" Content = 'Item3'/>  
        <TextBox />      
    </StackPanel>
</Window>
"@
 
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )

$Item1 = $Window.FindName('Item1')
$Item2 = $Window.FindName('Item2')
$Item3 = $Window.FindName('Item3')

$Window.Showdialog() | Out-Null

image

With this approach, we can check 1 or more of these checkboxes.

image

With these items checked, we can check the IsChecked property for each checkbox to see that they have been checked which is great when using a comparison operator such as –eq or –ne.

image

So far, we have seen how there are two states: checked and unchecked. But there is also another state that you can use which requires you to set the property IsThreeState = ‘True’ on the checkbox control. By doing so, you now get something like this:

image

What do you think will happen when we look at the IsChecked properties of each checkbox, especially $Item2.

image

In this case, the checkbox with the alternate checkbox doesn’t have a True or False property, it is simply Null. Something to keep in mind when working with the IsThreeState property.

I have covered a couple of the more useful properties, now it is time to look at a few events that relate to each of the types of checkbox IsChecked properties.

Using the Checked event, whenever the checkbox is set to the IsChecked=$True property, an event can kick off. Same goes for the Unchecked event with IsProperty=$False. Indeterminate event only works when the checkbox is not a checkbox and not unchecked.

I’ve added a couple more textboxes to handle each checkbox.

#Build the GUI
[xml]$xaml = @"
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
    SizeToContent = "WidthAndHeight" ShowInTaskbar = "True" Background = "lightgray"> 
    <StackPanel >
        <CheckBox x:Name="Item1" Content = 'Item1' IsThreeState = 'True' />
        <CheckBox x:Name="Item2" Content = 'Item2' IsThreeState = 'True' />
        <CheckBox x:Name="Item3" Content = 'Item3' IsThreeState = 'True' />  
        <TextBox x:Name='Item1_txt' />      
        <TextBox x:Name='Item2_txt' />      
        <TextBox x:Name='Item3_txt' />      
    </StackPanel>
</Window>
"@
 
$reader=(New-Object System.Xml.XmlNodeReader $xaml)

#Connect to Controls
$Window=[Windows.Markup.XamlReader]::Load( $reader )

$Item1 = $Window.FindName('Item1')
$Item2 = $Window.FindName('Item2')
$Item3 = $Window.FindName('Item3')
$Item1_txt = $Window.FindName('Item1_txt')
$Item2_txt = $Window.FindName('Item2_txt')
$Item3_txt = $Window.FindName('Item3_txt')

#Events
## Item1
$Item1.Add_Checked({
    $Item1_txt.Text = "$($This.name) State: Checked"
})
$Item1.Add_UnChecked({
    $Item1_txt.Text = "$($This.name) State: Unchecked"
})
$Item1.Add_Indeterminate({
    $Item1_txt.Text = "$($This.name) State: Indeterminate"
})

## Item2
$Item2.Add_Checked({
    $Item2_txt.Text = "$($This.name) State: Checked"
})
$Item2.Add_UnChecked({
    $Item2_txt.Text = "$($This.name) State: Unchecked"
})
$Item2.Add_Indeterminate({
    $Item2_txt.Text = "$($This.name) State: Indeterminate"
})

## Item3
$Item3.Add_Checked({
    $Item3_txt.Text = "$($This.name) State: Checked"
})
$Item3.Add_UnChecked({
    $Item3_txt.Text = "$($This.name) State: Unchecked"
})
$Item3.Add_Indeterminate({
    $Item3_txt.Text = "$($This.name) State: Indeterminate"
})


$Window.Showdialog() | Out-Null

 

image

If you are looking to have a group of checkboxes in which only one can be selected in the group, then you have 2 options:

  • Create an event handler to manually uncheck the other checkboxes when a checkbox is checked
  • Use the RadioButton control instead in which this is the controls purpose.

I won’t cover the RadioButton here, but will show a quick example of using a event to to uncheck other checkboxes when one has been checked.

I removed the IsThreeState property from this round and will only focus on the two possible options. Rather than make an event for each checkbox, I am going to make a single event scriptblock and apply it to each control.

#Events
## Checked Events
[System.Windows.RoutedEventHandler]$Script:CheckBoxChecked = {
    $Window.Content.Children | Where {
        $_ -is [System.Windows.Controls.CheckBox] -AND $This.Name  -ne $_.Name
    } | ForEach {
        $_.IsChecked = $False
    }
}
$Window.Content.Children | Where {
    $_ -is [System.Windows.Controls.CheckBox]
} | ForEach {
    $_.AddHandler([System.Windows.Controls.CheckBox]::CheckedEvent, $CheckBoxChecked)
}

Here I define the scriptblock to look for all checkboxes which do not match the name of the checkbox that had been clicked to show a checkbox. From there, I go through each checkbox and set the IsChecked property to $False. After applying each of these event scriptblocks to all of my Checkbox controls, we are set to try and click each checkbox to see if I can have more than one at a time.

image

That is all to working with the Checkbox control using PowerShell and WPF!

This entry was posted in powershell, WPF and tagged , , . Bookmark the permalink.

7 Responses to PowerShell and WPF: Checkboxes

  1. Richard says:

    sorry i reply to my comment …
    $Font = New-Object System.Drawing.Font(“Times New Roman”,18,[System.Drawing.FontStyle]::Italic)
    $CheckBox.Font = $Font

    Regards

  2. Richard says:

    Hi,
    It’s possible to change font size of checkbox ?
    Regards

  3. Pingback: Trying to find documentation for using GUI elements in Powershell - How to Code .NET

  4. Vineet Chanchal says:

    I want to make a clear button which will uncheck all the checkboxes and radio buttons in my powershell ui

  5. Bill says:

    Hi I’m new with Powershell,
    Today I create a form with Visual Studio, with different textboxes, and combobox.
    This works fine.
    But there’s on checkbox in this form.
    And I like it, when this one is checked, It create an new variable, which one I later need in my code for example:
    $WPFchBox.IsChecked if( { $NewVariable = “yes”})
    Can one of you drive in the right direction?

  6. Pingback: PowerShell and WPF: Radio Button | Learn Powershell | Achieve More

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s