The problem is: normally the children of a user controls are private or don't have member declarations at all (the declaration will appear if you add a
Name
attribute in XAML), so they are not directly visible to the form or other container owning an instance of your user control.
That said, you need to expose some interface of the user control to the code using it. With the state of check box, it's simple:
partial class MyUserControl {
CheckBox myCheckBox;
public bool? IsMyCheckBoxChecked {
get { return myCheckBox.IsChecked; }
set { myCheckBox.IsChecked = value; }
}
}
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked.aspx[
^].
You did not ask about exposing the events, but I will give you the idea.
If will require a bit more thinking if you want to expose the event, such as
Checked
event:
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click.aspx[
^].
You will need to declare an event in
MyUserControl
(let's call it
MyUserControl.MyCheckBoxCheckedChanged
), then add an event handler to the invocation list
myCheckBox.Checked
. This event handler should invoke your newly created event instance
MyUserControl.MyCheckBoxCheckedChanged
and, say, pass the checked state of the check box to the event arguments. When you add an event handler of
MyUserControl.MyCheckBoxCheckedChanged
in the window code, the handler will receive event arguments with the information on the check box state after the click.
When this is done, you will be able to bind corresponding properties or events of your user control in exact same ways as with available FCL controls.
—SA