绑定到包含自定义控件的userControl

时间:2013-05-25 12:45:22

标签: c# wpf binding user-controls custom-controls

我想使用WPF Spark project

ToggleSwitch控件

所以我创建了一个UserControl,其中包含一个ToggleSwitch控件并对其进行配置(颜色,大小等)。

<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>

ToggleSwitchCustomControl,它会覆盖标准WPF ToggleButton

现在我想在我的ToggleButton中使用IsChecked属性XAML进行绑定。

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

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

在代码隐藏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));

并将其绑定到IsChecked的{​​{1}}属性:

ToggleSwitch

完成此操作后,您将能够:

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

答案 1 :(得分:1)

您可以使用依赖项属性。

在自定义控件中添加依赖项属性,如下所示:

    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); }
    }