开发者

How to set a Checkbox checked property to true

I want to se开发者_StackOverflowt the default property of a checkbox to true


I suppose you only mean that on opening a form, one or more check boxes are checked .

Simply write in the Form_Load method

private void Form_Loaded (object sender, RoutedEventArgs e) {
    CheckBox1.IsChecked = true;
}


chkEntregue.CheckState = CheckState.Checked;


Set the Checked property to True in the Properties window of Visual Studio at design time.


To set CheckBox:

 CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, true);

To unset CheckBox:

 CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, false);


You haven't specified which platform, so I'll answer this for WPF, in which it's definitely possible.

You can use the OverrideMetadata method on CheckBox.IsCheckedProperty to provide a default value of "true" for all CheckBoxes. Add this code to your App class (in App.xaml.cs):

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    CheckBox.IsCheckedProperty.OverrideMetadata(typeof(CheckBox),
        new FrameworkPropertyMetadata(true));
}


For a given checkbox? Edit the form in the forms designer, and change the Checked property to true.

For all checkboxes in the environment, without changing each individually? Can't be done. Though I suppose if you got really ambitious, you could write a post-compiler or some such.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜