i have investigated this problem but this is solved in design view and code-behind. but my problem is little difference: i try to do this as only code-behind because my checkbox
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;
}
}
}