Dean Movy
Dean Movy

Reputation: 139

How to programmatically click a CheckBox in 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();

Upvotes: 3

Views: 956

Answers (1)

mm8
mm8

Reputation: 169190

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;

Upvotes: 5

Related Questions