Amit C

Amit C

  • NA
  • 3
  • 6.9k

Select All check box issue in MVVM pattern

Sep 14 2010 6:57 AM
I have problem implementing 'Select All' checkbox in SilverLight and MVVM design pattern.
 
<ListBox Name="PARENTLISTBox" ItemsSource="{Binding ParentList}">                        ------ Parent list box
<ListBox.ItemTemplate>
<DataTemplate>
<stackPanel orientation= "Vertical">
                            <TextBlock x:Name="txtblkParentName" Text="{Binding ParentName}" />
 
<CheckBox x:Name="SelectAllCheckBox" Content="Select All"
                                                      CommandParameter="{Binding ElementName= SelectAllCheckBox }"
                                                      Command="{Binding ElementName= PARENTLISTBox, Path=SelectAllCommand, Mode=TwoWay}"/>
                    </StackPanel>
 
<ListBox Name="CHILDLIST" ItemsSource="{Binding ChildProperty}" >   ------  child List box
                                 <ListBox.ItemTemplate>
                                                  <DataTemplate>
                                                       <StackPanel Orientation="Horizontal" Name="stkpanel">
                                                                <CheckBox Name="childCheckBox" Content="{Binding ChildName}"
                                                                         
                                                                          IsChecked="{Binding ElementName= SelectAllCheckBox, Path=IsChecked}"
                                                                          CommandParameter="{Binding ElementName= childCheckBox }"
                                                                          Command="{Binding ElementName=ParentList, Path=ChildCheckBoxCommand, Mode=TwoWay}"  />
                                                        </StackPanel>
                                                  </DataTemplate>
                                 </ListBox.ItemTemplate>
                         </ListBox>
                               </DataTemplat>
        <ListBox.ItemTemplate>
   </ListBox>
 
The output is something like:
 
                                Father    [ ]Select all
                                 [ ]  Child 1
                                 [ ] Child 2
                                 [ ] Child 3
 
                                Mother  [ ]Select all
                                  [ ] Child 4
                                  [ ] Child 5
                                  [ ] Child 6
 
                                Grandfather     [ ]Select all
                                    [ ] Child 7       
                                     ………..
                                    …………
 
If I check the Father checkbox, all the Childs should get selected under father. This is happening
 
But
Problem- 1 if I unselect any child, the 'select All' is still kept as selected --    this should not happen.
Problem 2- if I select/check on Father,……. child1,2,3 get selected, but if I un-select/un-check    any child say  … child2 or child 1 then un-select Father(select All) and then again select/check Father, child 2 /child 1 remain un-selected/un-checked.
 
Problem 3 – on the select/check of 'select all' I want to add all the child in a arrayList/collection and if I unselect any child, that child should get removed from the childlist…
 
please suggest me how to proceed with this issue in MVVM pattern.

Answers (2)