MVVMLight模式全选与反选

view部分

<Window x:Class="MVVMLightTest.View.CheckBoxTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVMLightTest.View"
        mc:Ignorable="d"
        DataContext="{Binding Source={StaticResource Locator}, Path=CheckedBox}"
        Title="CheckBoxTest" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0">
            <CheckBox Content="全选" Command="{Binding SelectedAllCommand}" IsChecked="{Binding IsSelectAll}"/>
            <TextBlock Text="已选中:"/>
            <TextBlock Text="{Binding Count}"/>
        </StackPanel>

        <Grid Grid.Row="1">
            <ListBox ItemsSource="{Binding StudentList}">
                <ListBox.Template>
                    <ControlTemplate TargetType="{x:Type ListBox}">
                        <WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
                    </ControlTemplate>
                </ListBox.Template>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox 
                            HorizontalAlignment="Left" Width="50" 
                            Content="{Binding Name}" IsChecked="{Binding IsSelected}"
                            Command="{Binding DataContext.SelectedCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

            
            <!--<ItemsControl ItemsSource="{Binding StudentList}" x:Name="cbt">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox HorizontalAlignment="Left" Width="50" Content="{Binding Name}" IsChecked="{Binding IsSelected}"
                                                 Command="{Binding DataContext.SelectedCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}"/>

                       </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>-->
        </Grid>
    </Grid>
</Window>

model部分

public class Student : ObservableObject
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; RaisePropertyChanged(() => Name); }
        }

        private bool _isselected;

        public bool IsSelected
        {
            get { return _isselected; }
            set { _isselected = value; RaisePropertyChanged(() => IsSelected); }
        }
    }

viewModel部分

public class CheckBoxViewModel : ViewModelBase
    {


        private ObservableCollection<Student> _selectedCollect;

        public ObservableCollection<Student> SelectedCollect
        {
            get { return _selectedCollect; }
            set { _selectedCollect = value; RaisePropertyChanged(() => SelectedCollect); }
        }


        private int _count;

        public int Count
        {
            get { return _count; }
            set { _count = value; RaisePropertyChanged(() => Count); }
        }



        private bool _isSelectAll;

        public bool IsSelectAll
        {
            get { return _isSelectAll; }
            set { _isSelectAll = value; RaisePropertyChanged(() => IsSelectAll); }
        }



        private ObservableCollection<Student> _studentList  ;

        public ObservableCollection<Student> StudentList
        {
            get { return _studentList; }
            set { _studentList = value; RaisePropertyChanged(() => StudentList); }
        }


        public RelayCommand SelectedAllCommand { get; set; }
        public RelayCommand SelectedCommand { get; set; }


        public CheckBoxViewModel()
        {
            
            StudentList = new ObservableCollection<Student>();
            StudentList.Add(new Student() { Name = "张三" });
            StudentList.Add(new Student() { Name = "李四" });
            StudentList.Add(new Student() { Name = "王五" });
            StudentList.Add(new Student() { Name = "赵六" });
            StudentList.Add(new Student() { Name = "周六" });

            SelectedAllCommand = new RelayCommand(OnSelectedAllCommand);
            SelectedCommand = new RelayCommand(OnSelectedCommand);

        }

        private void OnSelectedCommand()
        {
            Count = 0;
            SelectedCollect = new ObservableCollection<Student>();

            if (StudentList != null && StudentList.Count != 0) 
            {
                var list = StudentList.Where(p => p.IsSelected);
                if (list.Count() > 0) 
                {
                    foreach (var item in list) 
                    {
                        SelectedCollect.Add(item);
                        Count++;
                    }
                }
                if (SelectedCollect.Count == StudentList.Count)
                {
                    IsSelectAll = true;
                }
                else 
                {
                    IsSelectAll = false;
                }
            }
        }

        private void OnSelectedAllCommand()
        {
            Count = 0;
            SelectedCollect = new ObservableCollection<Student>();

            if (IsSelectAll)
            {
                foreach (var item in StudentList)
                {
                    item.IsSelected = true;
                    SelectedCollect.Add(item);
                    Count++;
                }
            }
            else 
            {
                foreach (var item in StudentList)
                {
                    item.IsSelected = false;
                }
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值