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

XML
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ToggleButton Width="100" Height="60" Content="togglebutton" IsChecked="True"></ToggleButton>
    </Grid>
         <local:UserControl1 HorizontalAlignment="Left" Margin="233,256,0,0" VerticalAlignment="Top"/>
</Window>



XML
<UserControl x:Class="WpfApplication2.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <CheckBox IsChecked="True" Width="100" Content="checkbox" Margin="100,125,100,150"/>
    </Grid>
</UserControl>
Posted
Updated 15-Apr-13 7:27am
v2

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:
C#
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: 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
 
Share this answer
 
v2
Assuming they don't share the same view model nor MainWindow contains UserControl1s' ViewModel, you can still do it with DelegateCommand http://wpftutorial.net/DelegateCommand.html
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