Question

I have a lot of check boxes in my WPF form. I want to get the selected checkbox value alone. In Winforms we can use foreach(checkbox ck in controls), but I cannot use like that in WPF Forms. How can i get the selected checkbox in WPF?

Was it helpful?

Solution

First of all, WPF is not just another replacement for WinForms, So the tricks in Winforms might be little different than WPF. WPF is all about DataBinding, so read about MVVM pattern which will really help you in WPF development. Now coming to the way to go with MVVM approach fort this, Imagine your ViewModel class contains a collection of bool. Now the DataTemplate has CheckBox.IsChecked property bind to the boolean, So when you change the checkbox the collection will hold the changed booleans appropriately.

public List<bool> MyBoolCollection{get; set;}

<ItemsControl ItemsSource="{Binding MyBoolCollection}" ...>
   <ItemsControl.ItemTemplate>
      <DataTemplate>
          <CheckBox IsChecked="{Binding}"/>
      </DataTemplate>
   </ItemsControl.ItemTemplate>

OTHER TIPS

Can you data bind the IsChecked property for each CheckBox? Bind it to a member variable of the container class. At least this way you can iterate over the member variables to determine if any are checked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top