2010-03-25 20 views
10

Ekli:Stil Tetik böyle kendi Ekli Mülkiyet oluşturduk Mülkiyet

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

Sonra buna bağlıdır tetikleyici içeren bir stil oluşturmak çalışıyorum:

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

Ama çalışma zamanı istisnası alıyorum:

Nasıl bir stil tetikleyici ekli özelliği değerini erişebilir

Cannot convert the value in attribute 'Property' to object of type 'System.Windows.DependencyProperty'. Error at object 'System.Windows.Trigger' in markup file 
? Bir RelativeSource bağlayıcısına sahip bir DataTrigger kullanmayı denedim, ancak değeri çekmiyordu.

cevap

15

Tetikleyici beyanınız gayet iyi, ancak ekli mülk bildiriminizde sorun var. Bir bağımlılık özelliğinin sahip tipi, 'un eklemeye planladığınız türden değil, bildirdiği tür olmalıdır. Yani bu:

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

Buna değiştirmek gerekiyor:

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

sayesinde, sahibi türünü değiştirmek sorunu giderildi nesnesine ama neden anlamadığını çalıştı. –