简体   繁体   English

如何在WPF中设置复选框的初始状态(在XAML或代码中)?

[英]How do you set the initial state of a checkbox in WPF (either in XAML or code)?

I have two simple related controls on a form, a delete button and a confirm delete checkbox. 我在表单上有两个简单的相关控件,一个delete按钮和一个confirm delete复选框。

The idea is to protect against accidental deletions (don't worry, undo will be added later) but still allow the user to delete without annoying confirmations if they so wish. 这个想法是为了防止意外删除(不用担心,稍后会添加撤消),但如果他们愿意,仍然允许用户删除而不需要烦人的确认。

However, I want the initial state of the checkbox to be set. 但是,我希望设置复选框的初始状态。 The XAML code is currently: XAML代码目前是:

<CheckBox Margin="0 5 0 0"
          x:Name="chkConfirmDel"
          Checked="chkConfirmDel_Change">
    Confirm delete?
</CheckBox>

but I can't see any obvious property for forcing the initial state, either in XAML or in the Window_Loaded() code. 但我无法在XAML或Window_Loaded()代码中看到强制初始状态的任何明显属性。

IsChecked="True"

XAML: XAML:

<!-- Initialized checked -->
<CheckBox x:Name="cbxSwitch" IsChecked="True" Content="Simple switch" />

<!-- Initialized in third state -->
<CheckBox x:Name="cbxSwitch3" Content="3 state switch"
    IsThreeState="True" IsChecked="{x:Null}" />

CS: CS:

// Two-state CheckBox doesn't require checking null
if (cbxSwitch.IsChecked == true)
{
    // Do something if selected
}

// In three-state CheckBox, IsChecked may be true, false or null
if (cbxSwitch3.IsChecked ?? false)
{
    // Do something only if IsChecked==true (selected)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM