views:

278

answers:

2

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?

A: 

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.

kmontgom
+1  A: 

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>
Jobi Joy
hai jobi, thanks, i got u wat saying abt. sorry ya, am jus new to wpf.
deep
+1 for the "not just another replacement" part
Arcturus
totally agree. wpf noobs (if i may stereotype) approach wpf as though it is winform's other half... but its not. its winform's super ex-girlfriend!
Tri Q