Click here to Skip to main content
15,748,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! Tell me how to make a binding:for example - at click in the main window ToggleButton to change CheckBox property IsChecked in UserControl

<Window x:Class="WpfApplication2.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <ToggleButton Width="100" Height="60" Content="togglebutton" IsChecked="True"></ToggleButton>
         <local:UserControl1 HorizontalAlignment="Left" Margin="233,256,0,0" VerticalAlignment="Top"/>

<UserControl x:Class="WpfApplication2.UserControl1"
             d:DesignHeight="300" d:DesignWidth="300">
        <CheckBox IsChecked="True" Width="100" Content="checkbox" Margin="100,125,100,150"/>
Updated 15-Apr-13 7:27am

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; // will appear if in XAML you add the attribute Name="myCheckBox", it will be private by default
    // actually, the declaration will appear in the auto-generated part of the partial declaration

    // ...

    public bool? IsMyCheckBoxChecked { 
        get { return myCheckBox.IsChecked; }
        set { myCheckBox.IsChecked = value; }
    // not that the property type is nit bool but bool?: the third bool? value, null,
    // will be used if myCheckBox.IsThreeState property is set to true


Please see:[^].

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:[^].

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.

Share this answer
Assuming they don't share the same view model nor MainWindow contains UserControl1s' ViewModel, you can still do it with DelegateCommand
there is others solutions… the "cleaner" way depends on context.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900