Enable Unclick for Radio Button & 1 selected Checkbox in Silverlight

Zhen Yuwang, 20 April 2012

Sometimes, when we insert a Radio Button or Checkbox into Date Grid in Silverlight, we could want several extra properties beside those basic ones. For example, there is a task that asks for enabling unclicks in Radio Button and allows only 1 selected in Checkbox. Let me show you how to implement them.

First of all, we create a Silverlight Application with Radio Button and Checkbox in a Date Grid.
 Enable Unclick for Radio Button and 1 selected Checkbox in Silverlight

Then, we need a Model used as ItemSource for these 2 containers. Assume we have a People like this:

    public class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Phone { get; set; }
        public int Tag { get; set; }
    }

Add several People instances with unique Tag value into a List<People> and set this List as ItemSource of Radio Button and Checkbox. 

Add "Click="radioButton_Click" Tag="{Binding Tag}"  to the XAML and these codes to .cs file

bool[] checkList = new bool[] { false, false, false };
private void radioButton_Click(Object sender, RoutedEventArgs e)
{
    RadioButton rb = sender as RadioButton; 

    int index = Int32.Parse(rb.Tag.ToString());    // Get Tag value
    bool isCheck = checkList[index];               // Get current value in checkList
    for (int i = 0; i < checkList.Length; i++)     // Set all in checkList to false
    {
        checkList[i] = false;
    }
    if (isCheck == true)                           // If current is true, 
    {
        checkList[index] = false;                  // Set current to false
        rb.IsChecked = false;                      // Set Radio Button NOT checked
    }
    else                                           // Else
    {
        checkList[index] = true;                   // Set current to true
    }
}

The Radio Button works like this.

Click to check, and then click again to uncheck. 
 Enable Unclick for Radio Button and 1 selected Checkbox in Silverlight

Enable Unclick for Radio Button and 1 selected Checkbox in Silverlight

Add Checked="checkBox_Checked" Tag="{Binding Tag}" to the XAML and these codes to .cs file

private void checkBox_Checked(Object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;

     int index = Int32.Parse(cb.Tag.ToString());        // Get Tag value
List<DataGridRow> rows = GetVisualChildCollection<DataGridRow>(checkBox);    
// Get all rows of Data Grid 
    foreach (DataGridRow row in rows)
    {
        FrameworkElement cellContent = checkBox.Columns[0].GetCellContent(row);  
// Get each CheckBox 
        CheckBox chk = cellContent as CheckBox;
        if (Int32.Parse(chk.Tag.ToString()) != index)  // If NOT current CheckBox, 
        {
            chk.IsChecked = false;                     // Set it to NOT checked
        }
    }
}

The Checkbox works like this.

Check 1 Checkbox will uncheck all other Checkbox.
 Enable Unclick for Radio Button and 1 selected Checkbox in Silverlight

Enable Unclick for Radio Button and 1 selected Checkbox in Silverlight

Enable Unclick for Radio Button and 1 selected Checkbox in Silverlight