Click here to Skip to main content
15,916,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have problem implementing "Select All" checkbox in SilverLight and MVVM design pattern.
XML
<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.
Posted
Updated 14-Sep-10 1:31am
v3

You will need to write some code to set the SelectAllCheckBox property to false when one of the children check boxes is unchecked.
 
Share this answer
 
SQL
Hi,

if I follow as above:

IsChecked="{Binding ElementName=SelectAllCheckBox, Path=IsChecked, Mode=TwoWay}"

If i do a TwoWay mode and unselect any child say child-1, then .....  select all, child 2,3,    ......   all get unselected.

Also if i Bind the IsChecked property of 'select all' checkbox, to any property in my Viewmodel and select all father, then Mother, grandfather also get selected (since select all is in a list box and repeats with the number of data row in  'ParentList'
.

plz help
 
Share this answer
 
I can't solve your entire problem, but I can give you several hints:

First, it makes no sense to use TwoWay binding for the Command property.

Second, try using TwoWay binding for the IsChecked property of the child checkbox:
XAML
IsChecked"{Binding ElementName=SelectAllCheckBox, Path=IsChecked, Mode=TwoWay}"


Third, read a book or an article about the basics of WPF commands and bindings...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900