CheckBox
1 2 3 4 5
| <CheckBox Content="苹果" IsChecked="True" IsEnabled="False" Style="{StaticResource ZCheckBoxStyle}" />
|
设置选中
在WPF中,要设置CheckBox的选中状态,可以通过CheckBox的IsChecked属性进行操作。IsChecked属性是一个可空的bool类型,可以设置为true表示选中,设置为false表示未选中,设置为null表示不确定状态。
你可以通过以下方式设置它的选中状态:
1
| <CheckBox x:Name="myCheckBox" IsChecked="True" Content="CheckBox"/>
|
或者,在代码中动态设置:
1 2
| myCheckBox.IsChecked = true; myCheckBox.IsChecked = false;
|
选中监听
在WPF中,你可以通过订阅CheckBox的Checked和Unchecked事件来监听其选中状态的变化。当CheckBox的选中状态发生变化时,Checked事件将被触发,而当取消选中时,Unchecked事件将被触发。
XML中处理
以下是一个简单的示例,演示如何在WPF中监听CheckBox的选中状态变化:
1
| <CheckBox x:Name="myCheckBox" Content="CheckBox" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
|
在代码中,你需要为Checked和Unchecked事件分别添加处理方法:
1 2 3 4 5 6 7 8 9 10 11
| private void CheckBox_Checked(object sender, RoutedEventArgs e) { MessageBox.Show("CheckBox被选中了!"); }
private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { MessageBox.Show("CheckBox被取消选中了!"); }
|
这样,当CheckBox的选中状态发生变化时,对应的事件处理方法就会被调用,你可以在这些方法中执行你需要的操作。
代码中处理
1 2 3 4 5 6 7 8
| MicrophoneCb.Checked += (sender, args) => {
}; MicrophoneCb.Unchecked += (sender, args) => {
};
|
或者
1 2 3 4 5 6
| MicrophoneCb.Checked += MicrophoneCbCheckChange; MicrophoneCb.Unchecked += MicrophoneCbCheckChange; private void MicrophoneCbCheckChange(object sender, EventArgs e) {
}
|
自定义样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| <Style x:Key="ZCheckBoxStyle" TargetType="{x:Type CheckBox}"> <Setter Property="SnapsToDevicePixels" Value="true" /> <Setter Property="OverridesDefaultStyle" Value="False" /> <Setter Property="FocusVisualStyle" Value="{DynamicResource CheckBoxFocusVisual}" /> <Setter Property="Height" Value="20" /> <Setter Property="IsChecked" Value="False" />
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="CheckBox"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Rectangle x:Name="CheckBoxRectangle" Fill="White" Opacity="0.3" RadiusX="2" RadiusY="2" /> <Rectangle x:Name="CheckBoxRectangleOut" Width="18" Height="18" RadiusX="2" RadiusY="2" Stroke="#DCDDDE" StrokeThickness="1" /> <Border x:Name="CheckedMark" Width="18" Height="18" Background="#2D8CF0" CornerRadius="2" Visibility="Collapsed"> <Grid Width="18" Height="18"> <Path Data="M3,9 L9,14" SnapsToDevicePixels="False" Stroke="White" StrokeThickness="2" /> <Path Data="M8,14 L15,4" SnapsToDevicePixels="False" Stroke="White" StrokeThickness="2" /> </Grid> </Border> </Grid> <TextBlock Grid.Column="1" Margin="4,0,0,0" VerticalAlignment="Center" FontSize="14" Foreground="#333" Text="{TemplateBinding Content}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="CheckedMark" Property="Visibility" Value="Visible" /> <Setter TargetName="CheckBoxRectangle" Property="Fill" Value="White" /> <Setter TargetName="CheckBoxRectangle" Property="Opacity" Value="1" /> <Setter TargetName="CheckBoxRectangleOut" Property="Stroke" Value="#2D8CF0" /> </Trigger>
<Trigger Property="IsEnabled" Value="False"> <Setter TargetName="CheckedMark" Property="Background" Value="#ddd" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
|