使用MVVM模式选择“所有复选框"问题 [英] Select All Checkbox issue using MVVM pattern

查看:182
本文介绍了使用MVVM模式选择“所有复选框"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SilverLight和MVVM设计模式中实现全选"复选框时遇到问题.

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>


输出类似于:

父亲[]选择全部
[]孩子1
[]儿童2
[]儿童3

母亲[]全选
[]儿童4
[]小孩5
[]儿童6

祖父[]全选
[]儿童7
………..
…………

如果我选中父亲"复选框,则应在父亲"下选中所有孩子".这正在发生

但是
问题-1如果我未选择任何孩子,则全选"仍保持选中状态-不会发生.
问题2-如果我选择/选中父亲,……. child1,2,3被选中,但是如果我取消选择/取消选中任何孩子,说…child2或child 1,然后取消选择父亲(全选),然后再次选择/选中父亲,则孩子2/child 1保持未选中状态-selected/unchecked.

问题3 –在全选"的选择/检查中,我想在arrayList/collection中添加所有子项,如果我取消选择任何子项,则该子项应从子项列表中删除...

请为我建议如何以MVVM模式处理此问题.


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.

推荐答案

您需要编写一些代码,以在子项之一检查时将SelectAllCheckBox属性设置为false.框未选中.
You will need to write some code to set the SelectAllCheckBox property to false when one of the children check boxes is unchecked.


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


我无法解决您的全部问题,但是我可以给您一些提示:

首先,对Command属性使用TwoWay绑定是没有意义的.
其次,尝试对子复选框的IsChecked属性使用TwoWay绑定:
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:
IsChecked"{Binding ElementName=SelectAllCheckBox, Path=IsChecked, Mode=TwoWay}"



第三,阅读有关WPF命令和绑定基础的书或文章...



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


这篇关于使用MVVM模式选择“所有复选框"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆