---
title: "checkbox like radiobutton wpf c#"  
description: "checkbox like radiobutton wpf c#"  
author: "Hank Greenberg"  
published: 2013-07-18  
updated: 2013-07-18  
canonical: https://www.mindstick.com/forum/1308/checkbox-like-radiobutton-wpf-c-sharp  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# checkbox like radiobutton wpf c#

HI [developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs)!

i have investigated this [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) but this is solved in [design view](https://answers.mindstick.com/qa/94814/design-view-is-not-getting-displayed-in-my-android-studio-what-should-i-do) and code-behind. but my problem is little [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is): i try to do this as only code-behind because my checkboxes are dynamically created according to [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) data.In other words, number of my checkboxes is not stable. i want to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) only one [checkbox](https://www.mindstick.com/articles/291/how-should-use-checkbox-control-in-vb-dot-net) in [group](https://yourviews.mindstick.com/view/81323/adani-green-energy-group-bags-world-s-biggest-solar-bid) of checkboxes. when i clicked one checkbox,i want that ischecked [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) of other checkboxes become false.this is same property in radiobuttons. i take my checkboxes from a stackpanel in xaml side:

```
<StackPanel Margin="4" Orientation="Vertical"  Grid.Row="1" Grid.Column="1" Name="companiesContainer">            </StackPanel>my xaml.cs:using (var c = new RSPDbContext())        {            var q = (from v in c.Companies select v).ToList();            foreach (var na in q)            {                CheckBox ch = new CheckBox();                ch.Content = na.Name;                ch.Tag = na;                companiesContainer.Children.Add(ch);            }        }foreach (object i in companiesContainer.Children)            {                CheckBox chk = (CheckBox)i;                chk.SetBinding(ToggleButton.IsCheckedProperty, "DataItem.IsChecked");            }
```

how can i provide this property in checkboxes in xaml.cs ?

thanks in advance..

## Replies

### Reply by shreesh chandra shukla

Solution!

Add an an event handler for the checked event. When creating the checkbox, add this (same) event handler to each checkbox.

In the event handler, run through each checkbox you've added, and for every checkbox, uncheck it UNLESS it's the same checkbox as the sender.

That I think should do the trick (off the top of my head).

## Here is some code I just knocked up that should help:

XAML part is just a stack panel called: Name="checkboxcontainer"

Codebehind part:

```
    public MainWindow()    {        InitializeComponent();        this.Loaded += MainWindow_Loaded;    }    private void MainWindow_Loaded(object sender, RoutedEventArgs e)    {        CreateCheckboxes();    }    private void CreateCheckboxes()    {        for (int i = 0; i <= 5; i++)        {            CheckBox c = new CheckBox();            c.Name = "Check" + i.ToString();            c.Checked += c_Checked; //This is adding the event handler to the checkbox            checkboxcontainer.Children.Add(c);        }    }    // This is the event handler for the checkbox    private void c_Checked(object sender, RoutedEventArgs e)    {        foreach (var control in checkboxcontainer.Children)        {            if (control is CheckBox && control != sender)            {                CheckBox cb = (CheckBox)control;                cb.IsChecked = false;            }        }
```


---

Original Source: https://www.mindstick.com/forum/1308/checkbox-like-radiobutton-wpf-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
