<Window
x:Class="checkboxtest.MainWindow"
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:local="clr-namespace:checkboxtest"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical">
<CheckBox Name="checkbox" Content="java" IsChecked="{Binding ChkChecked}" />
<Button Click="Button_Click" Content="click" />
</StackPanel>
</Grid>
</Window>
using System.Windows;
namespace checkboxtest
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
public bool? ChkChecked { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
ChkChecked = !ChkChecked;
https://oomake.com/question/2211787
using System.ComponentModel;
using System.Windows;
namespace checkboxtest
public partial class MainWindow : Window, INotifyPropertyChanged
public MainWindow()
InitializeComponent();
this.DataContext = this;
private bool? chkChecked = false;
public bool? ChkChecked
return chkChecked;
chkChecked = value;
RaisePropertyChanged("ChkChecked");
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string _Prop)
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(_Prop));
private void Button_Click(object sender, RoutedEventArgs e)
ChkChecked = !ChkChecked;
必须同时有字段和属性, 否则启动报错
有时候会报如下空指针错误
代码修改为如下即可
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string prop)
if (null != prop && IsLoaded)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
#endregion INotifyPropertyChanged
错误版xaml<Window x:Class="checkboxtest.MainWindow" 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"
<CheckBox x:Name="cb1" Grid.Row="1" Margin="5,0,0,0"
Content="Three-state CheckBox" IsThreeState="True"
Checked="HandleCheck" Unchecked="HandleUnchecked"
Indeterminate="HandleThirdState" Background="Red"
1.CheckBox控件介绍
复选框 允许可以选择多个 ContentControl
常用属性 Content Name IsChecked(true false null) IsThreeState Tag
获取已勾选选项 父容器 —Grid Children —子元素的集合
2.具体案例
<Grid Name="grid1">
<!--中间状态时 IsChecked 空-->
<!--<CheckBox .
<Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyl
<Window x:Class="Treeview.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
现象:当一个对象集合里,某些属性需要以RadioButton或者CheckBox的形式展现出来,并且会随着切换集合中的对象而变化时,RadioButton绑定不会正确更新,而CheckBox则没问题;
这里以一个小例子来说明:
一个学生集合,每个学生其中包含一个性别属性S
如果控件有选择事件的话这一在XAML中直接IsChecked=true可能出问题
因为程序编译先执行初始化控件后执行窗体load等事件,所以在XAML中直接IsChecked=true便会在控件加载中便执行选择事件,再执行窗体事件和全局变量,很可能会报错,这时不要在XAML中直接IsChecked=true,而要在窗体load事件中手动写一下代码
<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;
希望能够帮到您!
flyingju:
https://mirror.bit.edu.cn/apache/skywalking/8.3.0/apache-skywalking-apm-es7-8.3.0.tar.gz
.net NLog
薛定谔的猫@: