2010-03-25 14 views
10

संलग्न मैं इस तरह अपने ही संलग्न संपत्ति बनाई है:शैली उत्प्रेरक संपत्ति

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

और फिर मैं एक ट्रिगर है कि यह इस पर निर्भर करता के साथ एक शैली बनाने के लिए कोशिश कर रहा हूँ:

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

लेकिन मुझे रन टाइम अपवाद मिल रहा है:

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

मैं शैली ट्रिगर में संलग्न संपत्ति के मूल्य तक कैसे पहुंच सकता हूं? मैंने एक रिलेटिवसोर्स बाध्यकारी के साथ डेटा ट्रिगर का उपयोग करने का प्रयास किया है, लेकिन यह मूल्य को खींच नहीं रहा था।

उत्तर

15

आपकी ट्रिगर घोषणा ठीक है, लेकिन आपकी संलग्न संपत्ति घोषणा में एक गड़बड़ है। एक निर्भरता संपत्ति के मालिक प्रकार को घोषित करने की आवश्यकता है, न कि जिस प्रकार से आप इसे संलग्न करने की योजना बना रहे हैं। तो यह:

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

यह करने के लिए बदलने की जरूरत:

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

धन्यवाद, मैं बाहर काम किया है कि मालिक प्रकार बदलने समस्या तय वस्तु के लिए, लेकिन मैं क्यों समझ में नहीं आया। –

संबंधित मुद्दे