title: How to get RadioButtons to work like ComboBox in MVVM pattern?
body: <p>In the following code, the <strong>user selects a customer from the ComboBox</strong> and that customer's information is displayed in the box. </p>
  
  <p>Now I also <strong>want to get RadioButtons to work</strong> with the same functionality. I've got the RadioButtons to display the ObservableCollection of Customers, but how can I get the <strong>IsChecked</strong> to work, i.e. <strong>what is the equivalent of <code>SelectedItem</code> for RadioButtons</strong>?</p>
  
  <p><strong>XAML:</strong></p>
  
  <p></p>
  
  <pre><code>&lt;Window.Resources&gt;
  
      &lt;DataTemplate x:Key="CustomerShowTemplate"&gt;
          &lt;Border CornerRadius="5" 
                  Background="#eee" 
                  Padding="5" 
                  HorizontalAlignment="Left"&gt;
              &lt;StackPanel&gt;
                  &lt;StackPanel Orientation="Horizontal"&gt;
                      &lt;TextBlock FontSize="14" FontWeight="Bold" Text="{Binding FirstName}"/&gt;
                      &lt;TextBlock Text=" "/&gt;
                      &lt;TextBlock FontSize="14" FontWeight="Bold" Text="{Binding LastName}"/&gt;
                  &lt;/StackPanel&gt;
                  &lt;TextBlock Text="{Binding Path=HireDate, 
                      StringFormat='Hired on {0:MMM dd, yyyy}'}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
  
      &lt;DataTemplate x:Key="CustomerComboBoxTemplate"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="{Binding FirstName}"/&gt;
              &lt;TextBlock Text=" "/&gt;
              &lt;TextBlock Text="{Binding LastName}"/&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  
      &lt;DataTemplate x:Key="CustomerRadioButtonTemplate"&gt;
          &lt;RadioButton GroupName="CustomerRadioButtonGroup" 
              IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"&gt;
              &lt;TextBlock Text="{Binding LastName}"/&gt;
          &lt;/RadioButton&gt;
      &lt;/DataTemplate&gt;
  
  &lt;/Window.Resources&gt;
  
  &lt;DockPanel LastChildFill="False" Margin="10"&gt;
  
      &lt;ContentControl 
          DockPanel.Dock="Top"
          Margin="0 0 0 10"
          Content="{Binding SelectedCustomer}" 
          ContentTemplate="{StaticResource CustomerShowTemplate}"/&gt;
  
      &lt;StackPanel 
          DockPanel.Dock="Top"
          Margin="0 0 0 10"&gt;
  
          &lt;ComboBox 
              ItemsSource="{Binding Customers}"
              ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
              Margin="20"
              HorizontalAlignment="Left"
              SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/&gt;
  
          &lt;ListBox 
              ItemsSource="{Binding Customers}"
              ItemTemplate="{StaticResource CustomerRadioButtonTemplate}" 
              SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/&gt;
  
  
          &lt;TextBlock Text="{Binding Customers.Count}"/&gt;
  
  
          &lt;Button Content="Add Customer" Command="{Binding AddCustomerCommand}"/&gt;
  
  
      &lt;/StackPanel&gt;
  
  &lt;/DockPanel&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>ViewModel:</strong></p>
  
  <pre><code>using System;
  using System.Collections.ObjectModel;
  using TestSelectedItem234.Models;
  
  namespace TestSelectedItem234.ViewModels
  {
      public class MainViewModel : ViewModelBase
      {
  
          #region ViewModelProperty: Customers
          private ObservableCollection&lt;Customer&gt; _customers = new ObservableCollection&lt;Customer&gt;();
          public ObservableCollection&lt;Customer&gt; Customers
          {
              get
              {
                  return _customers;
              }
  
              set
              {
                  _customers = value;
                  OnPropertyChanged("Customers");
              }
          }
          #endregion
  
          #region ViewModelProperty: SelectedCustomer
          private Customer _selectedCustomer;
          public Customer SelectedCustomer
          {
              get
              {
                  return _selectedCustomer;
              }
  
              set
              {
                  _selectedCustomer = value;
                  OnPropertyChanged("SelectedCustomer");
              }
          }
          #endregion
  
          #region ViewModelProperty: CustomerIsSelected
          private bool _customerIsSelected;
          public bool CustomerIsSelected
          {
              get
              {
                  return _customerIsSelected;
              }
  
              set
              {
                  _customerIsSelected = value;
                  OnPropertyChanged("CustomerIsSelected");
              }
          }
          #endregion
  
          public MainViewModel()
          {
              LoadCustomers();
  
              CustomerIsSelected = false;
          }
  
          private void LoadCustomers()
          {
              _customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", HireDate = DateTime.Parse("2007-12-31") });
              _customers.Add(new Customer { FirstName = "Jack", LastName = "Taylor", HireDate = DateTime.Parse("2005-12-31") });
              _customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", HireDate = DateTime.Parse("2005-06-30") });
              _customers.Add(new Customer { FirstName = "Alice", LastName = "Jones", HireDate = DateTime.Parse("2005-08-23") });
              _customers.Add(new Customer { FirstName = "Mary", LastName = "Ashton", HireDate = DateTime.Parse("2005-12-22") });
              _customers.Add(new Customer { FirstName = "Joe", LastName = "Jones", HireDate = DateTime.Parse("2005-11-22") });
              _customers.Add(new Customer { FirstName = "Henry", LastName = "Smith", HireDate = DateTime.Parse("2005-12-11") });
              _customers.Add(new Customer { FirstName = "Allison", LastName = "Rodrigez", HireDate = DateTime.Parse("2004-12-14") });
              _customers.Add(new Customer { FirstName = "Angela", LastName = "Thompson", HireDate = DateTime.Parse("2003-03-14") });
              _customers.Add(new Customer { FirstName = "Shawna", LastName = "Quaker", HireDate = DateTime.Parse("2005-12-14") });
  
              SelectedCustomer = _customers[0];
          }
  
      }
  }
  </code></pre>
  
  <h1>Update:</h1>
  
  <p>I also tried this from Thomas, but it still doesn't connect IsChecked with IsSelected:</p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding Customers}"&gt;
      &lt;ListBox.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt;
                          &lt;RadioButton Content="{Binding FirstName}" IsChecked="{Binding IsSelected, RelativeSource={x:Static RelativeSource.TemplatedParent}}"/&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/ListBox.ItemContainerStyle&gt;
  &lt;/ListBox&gt;
  </code></pre>