Click here to Skip to main content
15,891,431 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF Checkboxes - Strange Behaviour

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
18 Nov 2014CPOL2 min read 12.1K   1   2
Something to look out for in WPF

Introduction

Yesterday, I set out to make what sounded like a very simple change to an existing WPF project and it turned out to be far from simple.

The mission was a straight-forward one - add an "are you sure?" prompt when users changed the setting of a checkbox in certain circumstances and revert the checkbox to its original setting should they decide against making the change.

The software in question is a rather scruffy implementation of WPF (one of those where the developer had tried to treat WPF as Winforms) so this is maybe not a problem that would occur in a purer WPF implementation but nonetheless, it was a highly annoying one so I thought that I'd note it for anyone else who might run into a similar situation.

The obvious approach here was to apply the business logic in the Checked/Unchecked event handler and revert the checkbox's IsChecked state when appropriate.

I duly inserted the code and ran the code. To my amazement, the checkbox started to behave in a way that was almost (but not quite) opposite to what was expected. I just couldn't figure out what was going on.

Well, it was a big mess of a project so it seemed reasonable to assume that something was resetting the value but I searched to no avail.

Using the Code

To ensure that there was no outside interference, I created a fresh project and reduced everything to a single Checkbox:

XML
<CheckBox Name="cbTest" 
Checked="cbTestCheckChanged" Unchecked="cbTestCheckChanged"  />

In the code behind, to keep things simple, I just set the prompt to occur on any change:

C#
private void cbTestCheckChanged(object sender, RoutedEventArgs e)
{
    MessageBoxResult result = MessageBox.Show
    ("Are you sure?", "Please confirm", MessageBoxButton.YesNo);

    if (result == MessageBoxResult.No)
        {
        //Disengage the event handler so we don't retrigger it ...

                cbTest.Checked -= cbTestCheckChanged;
                cbTest.Unchecked -= cbTestCheckChanged;

                //Change the state of the box
                cbTest.IsChecked = !cbTest.IsChecked;

                //Re-engage the handler
                cbTest.Checked += cbTestCheckChanged;
                cbTest.Unchecked += cbTestCheckChanged;
       }
} 

All that worked exactly as I'd expect it to, but then I had a suspicion. What would happen if I applied a theme to the project?

I duly applied the WhistlerBlue theme and found that the stripped solution was suddenly behaving with all the erraticness of the original. A quick run through of the various Codeplex themes showed that this bug was common to all of them.

By changing the mark-up to ignore styles, I could make it work:

XML
<CheckBox Name="cbTest" Checked="cbTestCheckChanged" 
Unchecked="cbTestCheckChanged" Style="{x:Null}" />  

(This could also be achieved by setting the Style property to null in the code behind).

Well, that was a few hours wasted but at least I've learnt a lesson: if your WPF Checkboxes are misbehaving and you're using the WPF Theme pack, disable the theme before you investigate any further. The same thing, I'm pretty sure, will also apply to radio-buttons.

Points of Interest

Setting the OverridesDefaultStyle property will not work, the style must be nulled.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
Peejay Adams is a software architect and developer from the UK. He's been banging out code for about 20 years during which time he's mostly, but by no means exclusively, worked in C, C# and SQLServer.

Comments and Discussions

 
QuestionProblem only with Themes from Toolkit? Pin
johannesnestler18-Nov-14 3:29
johannesnestler18-Nov-14 3:29 
AnswerRe: Problem only with Themes from Toolkit? Pin
PeejayAdams18-Nov-14 3:57
PeejayAdams18-Nov-14 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.