2016-09-02 2 views
0

J'ai besoin d'aide pour les propriétés de dépendance. Salut, je veux accéder à deux propriétés qui sont définies dans un modèle de contrôle, CheckedText et UncheckedText:Accès à une propriété de dépendance dans un style personnalisé

<Style x:Key="ToggleCheckBoxStyle" TargetType="{x:Type CheckBox}"> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> 
    <Setter Property="Height" Value="30" /> 
    <Setter Property="Width" Value="110" /> 
    <Setter Property="packages:Variables.X" Value="0" /> 
    <Setter Property="FontFamily" Value="Segoe UI" /> 
    <Setter Property="FontWeight" Value="Bold" /> 
    <Setter Property="FontSize" Value="13" /> 
    <Setter Property="BorderBrush" Value="#FF939393" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="VerticalAlignment" Value="Center" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type CheckBox}"> 
       <Grid ClipToBounds="True"> 
        <Grid x:Name="Container"> 

         ... 

         <Border Height="{Binding Height, 
               RelativeSource={RelativeSource TemplatedParent}}" 
           HorizontalAlignment="Left" 
           Background="{TemplateBinding styles:ToggleCheckBox.CheckedBackground}"> 
          <Border.Width> 
           <MultiBinding Converter="{arithmeticConverter:ArithmeticConverter}" ConverterParameter="x-y"> 
            <Binding Path="Width" RelativeSource="{RelativeSource TemplatedParent}" /> 
            <Binding Path="(styles:ToggleCheckBox.ToggleWidth)" RelativeSource="{RelativeSource TemplatedParent}" /> 
           </MultiBinding> 
          </Border.Width> 
          <TextBlock HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             FontFamily="{TemplateBinding FontFamily}" 
             FontSize="{TemplateBinding FontSize}" 
             FontWeight="{TemplateBinding FontWeight}" 
             Foreground="{TemplateBinding styles:ToggleCheckBox.CheckedForeground}" 
             Text="{TemplateBinding styles:ToggleCheckBox.**CheckedText**}" /> 
         </Border> 
         <Border Width="{Binding Width, 
               RelativeSource={RelativeSource TemplatedParent}, 
               Converter={arithmeticConverter:ArithmeticConverter}, 
               ConverterParameter=x-20}" 
           Height="{Binding Height, 
               RelativeSource={RelativeSource TemplatedParent}}" 
           HorizontalAlignment="Left" 
           Background="{TemplateBinding styles:ToggleCheckBox.UncheckedBackground}"> 
          <Border.RenderTransform> 
           <TranslateTransform X="{Binding Width, RelativeSource={RelativeSource TemplatedParent}}" /> 
          </Border.RenderTransform> 
          <TextBlock HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             FontFamily="{TemplateBinding FontFamily}" 
             FontSize="{TemplateBinding FontSize}" 
             FontWeight="{TemplateBinding FontWeight}" 
             Foreground="{TemplateBinding styles:ToggleCheckBox.UncheckedForeground}" 
             Text="{TemplateBinding styles:ToggleCheckBox.**UncheckedText**}" /> 
         </Border> 
        </Grid> 
        <Border Background="Transparent" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          CornerRadius="1" /> 
       </Grid> 

Et voici la définition des propriétés de dépendance du ToggleCheckBox:

public class ToggleCheckBox 
{ 
    public static readonly DependencyProperty CheckedTextProperty = DependencyProperty.RegisterAttached("CheckedText", typeof(string), typeof(ToggleCheckBox), new FrameworkPropertyMetadata("ON")); 
    public static void SetCheckedText(UIElement element, string value) 
    { 
     element.SetValue(CheckedTextProperty, value); 
    } 

    public static string GetCheckedText(UIElement element) 
    { 
     return (String)element.GetValue(CheckedTextProperty); 
    } 

    public static readonly DependencyProperty UncheckedTextProperty = DependencyProperty.RegisterAttached("UncheckedText", typeof(string), typeof(ToggleCheckBox), new FrameworkPropertyMetadata("OFF")); 

    public static void SetUncheckedText(UIElement element, string value) 
    { 
     element.SetValue(UncheckedTextProperty, value); 
    } 

    public static string GetUncheckedText(UIElement element) 
    { 
     return (String)element.GetValue(UncheckedTextProperty); 
    } 
} 

Et dans l'utilisation à mon avis:

<CheckBox x:Name="IsDataStoreLocal" 
      HorizontalAlignment="Left" 
      Style="{StaticResource ToggleCheckBoxStyle}" 
      CheckedText="YES" 
      UnCheckedText="NO"/> 

Ce que je voudrais faire est quelque chose comme ce qui précède. Bien sûr, les propriétés ne sont pas reconnues, probablement parce que je n'ai qu'un style, pas un contrôle. Comment puis-je modifier les valeurs Checked et UncheckedText dans une nouvelle case en utilisant ce style?

Toute aide serait appréciée. Merci, Will

J'ai regardé des questions similaires, mais je ne peux pas trouver celui qui correspond à ce que j'essaie de faire.

+0

Voulez-vous dire que les propriétés ne sont pas reconnus dans l'utilisation '' ou dans le modèle? – ASh

+0

Dans l'utilisation. Je vais modifier le post. Je vous remercie. – Will

+0

essayez de l'utiliser en déclarant le type '' comme les autres propriétés attachées (par exemple 'Grid.Column'). – ASh

Répondre

0

pour définir une valeur propriété attachée sur un élément cible (checkBox), utilisez la syntaxe suivante:

namespacePrefix:ownerTypeName.propertyName 

en cas de votre propriété personnalisée:

<CheckBox styles:ToggleCheckBox.CheckedText="YES"/>