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?
views:
278answers:
2
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
2010-03-23 03:35:24
+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
2010-03-23 03:40:09
hai jobi, thanks, i got u wat saying abt. sorry ya, am jus new to wpf.
deep
2010-03-23 04:12:31
+1 for the "not just another replacement" part
Arcturus
2010-08-23 11:26:39
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
2010-10-05 05:04:51