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:

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

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.

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:

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

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.

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.

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.

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