Jul 28, 2011

WPF Data Binding Booleans: To Be or !To Be

The road leading towards WPF mastery hit a small bump when it came to data binding and Booleans. I wasn't surprised when my first attempt didn't work, I wasn't even sure what I expected to happen. Normally Boolean fields have some automated behavior. When they are grouped, clicking one button true sets the others in the group false. How does this play with data binding where the value for the field is supposed to be set by the binding?  It's enough to make me think if Scott Meyers's were writing a book on this he would have an item titled Prefer CheckBoxes. Checkboxes work with simple Binding to IsChecked just like you'd expect. I do have customers though that insist on Booleans because they like to see both options explicitly shown, so here's one solution:


Command Binding

Edit: Turns out you don't really need the command binding.  Just using two properties as described below is sufficient. I'm leaving the code snapshots as they were originally published but  you don't need to bind to the Command property and you don't need to write the two commands.

One way to get Booleans to behave with data binding is to bind not just IsChecked but also bind the Command as shown below. This XAML creates two radio buttons labeled To Be and Not To Be. In my final code I added a Mode=OneWay to the binding, but it appeared to work no differently with the default binding.

XAML Snippet
  1. <RadioButton Content="To Be"
  2.              IsChecked="{Binding ToBe}"
  3.              Command="{Binding ToBeCommand}" />
  4. <RadioButton Content="Not To Be"
  5.              IsChecked="{Binding NotToBe}"
  6.              Command="{Binding NotToBeCommand}" />

The ToBe Property is pretty standard except it also sets the property and raises the event for NotToBe. NotToBe is just an automatic property. Both commands will be implemented, but they will both set the ToBe property, one will set it to true and the other will set it to false. Here's the full code example:

Properties and ICommands
  1. private bool _toBe;
  2. public bool ToBe
  3. {
  4.     get { return _toBe; }
  5.     set
  6.     {
  7.         _toBe = value;
  8.         NotToBe = !value;
  9.         RaisePropertyChanged("ToBe");
  10.         RaisePropertyChanged("NotToBe");
  11.     }
  12. }
  14. public bool NotToBe { get; set; }
  16. private RelayCommand _toBeCommand;
  17. private RelayCommand _notToBeCommand;
  19. public ICommand ToBeCommand
  20. {
  21.     get { return _toBeCommand ?? (_toBeCommand = new RelayCommand(x => ToBeExecute())); }
  22. }
  24. private void ToBeExecute()
  25. {
  26.     ToBe = true;
  27. }
  29. public ICommand NotToBeCommand
  30. {
  31.     get { return _notToBeCommand ?? (_notToBeCommand = new RelayCommand(x => NotToBeExecute())); }
  32. }
  34. private void NotToBeExecute()
  35. {
  36.     ToBe = false;
  37. }

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.