wpf checkbox全选
时间: 2023-04-03 08:03:21 浏览: 267
您好,关于 WPF Checkbox 全选的问题,可以通过以下代码实现:
XAML 代码:
<StackPanel>
<CheckBox x:Name="cbAll" Content="全选" Checked="cbAll_Checked" Unchecked="cbAll_Unchecked"/>
<CheckBox Content="选项1" IsChecked="{Binding IsChecked, ElementName=cbAll, Mode=OneWay}"/>
<CheckBox Content="选项2" IsChecked="{Binding IsChecked, ElementName=cbAll, Mode=OneWay}"/>
<CheckBox Content="选项3" IsChecked="{Binding IsChecked, ElementName=cbAll, Mode=OneWay}"/>
</StackPanel>
C# 代码:
private void cbAll_Checked(object sender, RoutedEventArgs e)
{
foreach (var item in (sender as CheckBox).Parent.Children)
{
if (item is CheckBox && item != sender)
{
(item as CheckBox).IsChecked = true;
}
}
}
private void cbAll_Unchecked(object sender, RoutedEventArgs e)
{
foreach (var item in (sender as CheckBox).Parent.Children)
{
if (item is CheckBox && item != sender)
{
(item as CheckBox).IsChecked = false;
}
}
}
希望能够帮到您!
阅读全文