2010-03-25 14 views
10

He creado mi propio bien embargado así:Estilo Trigger en la propiedad adjunta

public static class LabelExtension 
    { 
     public static bool GetSelectable(DependencyObject obj) 
     { 
      return (bool)obj.GetValue(SelectableProperty); 
     } 
     public static void SetSelectable(DependencyObject obj, bool value) 
     { 
      obj.SetValue(SelectableProperty, value); 
     } 
     // Using a DependencyProperty as the backing store for Selectable. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty SelectableProperty = 
      DependencyProperty.RegisterAttached("Selectable", typeof(bool), typeof(Label), new UIPropertyMetadata(false)); 
    } 

Y entonces yo estoy tratando de crear un estilo con un disparador que depende de él:

<!--Label--> 
<Style TargetType="{x:Type Label}"> 
    <Style.Triggers> 
     <Trigger Property="Util:LabelExtension.Selectable" Value="True"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Label}"> 
         <TextBox IsReadOnly="True" Text="{TemplateBinding Content}" /> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

pero me estoy poniendo un tiempo de ejecución excepción:

Cannot convert the value in attribute 'Property' to object of type 'System.Windows.DependencyProperty'. Error at object 'System.Windows.Trigger' in markup file 

¿Cómo puedo acceder al valor de los bienes embargados en un disparador de estilo? He intentado usar un DataTrigger con un enlace RelativeSource pero no estaba pasando el valor.

Respuesta

15

Su declaración de activación está bien, pero su declaración de propiedad adjunta tiene un problema técnico. El tipo de propietario de una propiedad de dependencia debe ser del tipo que declara, no del tipo al que planea adjuntarlo. Así que esto:

DependencyProperty.RegisterAttached("Selectable", typeof(bool), typeof(Label)... 

tiene que cambiar a esto:

DependencyProperty.RegisterAttached("Selectable", typeof(bool), typeof(LabelExtension)... 
                   ^^^^^^^^^^^^^^^^^^^^^^ 
+0

Gracias, resultó que cambiar el tipo de propietario de objeto fijo el problema pero yo no entendía por qué. –

Cuestiones relacionadas