Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically click a CheckBox in WPF?

Tags:

checkbox

wpf

SO i have CheckBox and since there's no CheckBox.PerformClick() method in WPF, is there a way to click a WPF CheckBox programmatically?

I found this solution but this for Button only:

ButtonAutomationPeer peer = new ButtonAutomationPeer(someButton);
IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
invokeProv.Invoke();
like image 841
Dean Movy Avatar asked Dec 14 '25 17:12

Dean Movy


1 Answers

You may use the PatternInterface.Toggle interface to toggle the CheckBox:

CheckBoxAutomationPeer peer = new CheckBoxAutomationPeer(someCheckBox);
IToggleProvider toggleProvider = peer.GetPattern(PatternInterface.Toggle) as IToggleProvider;
toggleProvider.Toggle();

Or you can set the IsChecked property:

someCheckBox.IsChecked = !someCheckBox.IsChecked;
like image 62
mm8 Avatar answered Dec 16 '25 16:12

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!