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;
}
}
}
}