Mare Infinitus
Mare Infinitus

Reputation: 8172

Binding to a userControl which contains custom control

I want to use the ToggleSwitch control of the WPF Spark project

So I created a UserControl which contains a ToggleSwitch control and configures it (color, size, etc).

<UserControl x:Class="WpfControls.ToggleSwitch.MyToggleSwitchControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:toggleSwitch="clr-namespace:WpfControls.ToggleSwitch"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/WpfControls;component/ToggleSwitch/ToggleSwitch.Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <toggleSwitch:ToggleSwitch x:Name="Toggle"
                                   Width="54"
                                   Height="21"
                                   Margin="0"
                                   Background="Black"
                                   BorderThickness="2"
                                   CheckedForeground="White"
                                   CheckedText="Yes"
                                   CheckedToolTip=""
                                   CornerRadius="10"
                                   FontFamily="Tahoma"
                                   FontSize="10"
                                   FontWeight="Normal"
                                   IsCheckedLeft="False"
                                   Padding="0"
                                   ThumbBorderThickness="2"
                                   ThumbCornerRadius="21"
                                   ThumbGlowColor="Gray"
                                   ThumbShineCornerRadius="20,20,0,0"
                                   ThumbWidth="35"
                                   UncheckedForeground="Black"
                                   UncheckedText="No"
                                   UncheckedToolTip="No">
        </toggleSwitch:ToggleSwitch>
    </Grid>
</UserControl>

The ToggleSwitch is a CustomControl which overrides the standard WPF ToggleButton.

Now I want to use the ToggleButton property IsChecked in my XAML for Binding.

<toggleSwitch:MyToggleSwitchControl IsChecked="{Binding IsChecked}" />

How can I achieve that?

Upvotes: 0

Views: 556

Answers (2)

Rafal
Rafal

Reputation: 1091

Create in code-behind DependencyProperty:

    public bool IsToggleChecked
    {
        get { return (bool)GetValue(IsToggleCheckedProperty); }
        set { SetValue(IsToggleCheckedProperty, value); }
    }
    public static readonly DependencyProperty IsToggleCheckedProperty =
        DependencyProperty.Register("IsToggleChecked", typeof(bool), typeof(MyToggleSwitchControl), new PropertyMetadata(false));

and bind it to IsChecked property of your ToggleSwitch:

<toggleSwitch:ToggleSwitch IsChecked="{Binding RelativeSource={RelativeSource AncestorLevel=1,AncestorType=UserControl,Mode=FindAncestor}, Path=IsToggleChecked}"

After doing this you'll be able to do:

<toggleSwitch:MyToggleSwitchControl IsToggleChecked="{Binding IsChecked}" />

Upvotes: 1

Philipp Eger
Philipp Eger

Reputation: 2275

You could use an dependency property.

In your custom control add a dependency property like this:

    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.Register("IsChecked", typeof(bool),
        typeof(MyToggleSwitchControl), null);

    // .NET Property wrapper
    public bool IsChecked
    {
        get 
        { 
            return (bool)GetValue(IsCheckedProperty); 
        }
        set { SetValue(IsCheckedProperty, value); }
    }

Upvotes: 1

Related Questions