2013-05-25 82 views
0

我想用ToggleSwitch控制的WPF Spark project結合包含自定義控件

一個UserControl所以我創建了一個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

現在我想在我的XAML中使用ToggleButton屬性IsChecked進行綁定。

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

我該如何做到這一點?

回答

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

並將其綁定到你的ToggleSwitchIsChecked屬性:

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

這樣做了以後,你就可以做到:

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

我得到一個警告,這DP隱藏繼承成員切換按鈕 –

+0

的器isChecked我想我需要訪問ToggleSwitch控件我的用戶裏面莫名其妙。你能幫助我嗎? –

+0

您可以使用'新'關鍵詞來避免此警告,或者爲此DependencyProperty選擇除'IsChecked'之外的其他名稱。 – Rafal

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

你的意思是我的usercontrol的.xaml.cs文件嗎?這似乎並沒有工作,因爲我得到一個警告,這隱藏了ToggleButton的IsChecked屬性,我想設置 –

+0

是的,我的意思是.xaml.cs文件,但我認爲,Rafal的答案現在可以滿足您的需求。 –