title: MVVM: Binding radio buttons to a view model?
body: <p><strong>EDIT:</strong> Problem was fixed in .NET 4.0.</p>
  
  <p>I have been trying to bind a group of radio buttons to a view model using the <code>IsChecked</code> button. After reviewing other posts, it appears that the <code>IsChecked</code> property simply doesn't work. I have put together a short demo that reproduces the problem, which I have included below.</p>
  
  <p>Here is my question: Is there a straightforward and reliable way to bind radio buttons using MVVM? Thanks.</p>
  
  <p><strong>Additional information:</strong> The <code>IsChecked</code> property doesn't work for two reasons:</p>
  
  <ol>
  <li><p>When a button is selected, the IsChecked properties of other buttons in the group don't get set to <em>false</em>.</p></li>
  <li><p>When a button is selected, its own IsChecked property does not get set after the first time the button is selected. I am guessing that the binding is getting trashed by WPF on the first click.</p></li>
  </ol>
  
  <p><strong>Demo project:</strong> Here is the code and markup for a simple demo that reproduces the problem. Create a WPF project and replace the markup in Window1.xaml with the following:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="WpfApplication1.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"&gt;
      &lt;StackPanel&gt;
          &lt;RadioButton Content="Button A" IsChecked="{Binding Path=ButtonAIsChecked, Mode=TwoWay}" /&gt;
          &lt;RadioButton Content="Button B" IsChecked="{Binding Path=ButtonBIsChecked, Mode=TwoWay}" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Replace the code in Window1.xaml.cs with the following code (a hack), which sets the view model:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Windows;
  
  namespace WpfApplication1
  {
      /// &lt;summary&gt;
      /// Interaction logic for Window1.xaml
      /// &lt;/summary&gt;
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              this.DataContext = new Window1ViewModel();
          }
      }
  }
  </code></pre>
  
  <p>Now add the following code to the project as <code>Window1ViewModel.cs</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Windows;
  
  namespace WpfApplication1
  {
      public class Window1ViewModel
      {
          private bool p_ButtonAIsChecked;
  
          /// &lt;summary&gt;
          /// Summary
          /// &lt;/summary&gt;
          public bool ButtonAIsChecked
          {
              get { return p_ButtonAIsChecked; }
              set
              {
                  p_ButtonAIsChecked = value;
                  MessageBox.Show(string.Format("Button A is checked: {0}", value));
              }
          }
  
          private bool p_ButtonBIsChecked;
  
          /// &lt;summary&gt;
          /// Summary
          /// &lt;/summary&gt;
          public bool ButtonBIsChecked
          {
              get { return p_ButtonBIsChecked; }
              set
              {
                  p_ButtonBIsChecked = value;
                  MessageBox.Show(string.Format("Button B is checked: {0}", value));
              }
          }
  
      }
  }
  </code></pre>
  
  <p>To reproduce the problem, run the app and click Button A. A message box will appear, saying that Button A's <code>IsChecked</code> property has been set to <em>true</em>. Now select Button B. Another message box will appear, saying that Button B's <code>IsChecked</code> property has been set to <em>true</em>, but there is no message box indicating that Button A's <code>IsChecked</code> property has been set to <em>false</em>--the property hasn't been changed.</p>
  
  <p>Now click Button A again. The button will be selected in the window, but no message box will appear--the <code>IsChecked</code> property has not been changed. Finally, click on Button B again--same result. The <code>IsChecked</code> property is not updated at all for either button after the button is first clicked.</p>