2010-06-29 36 views
3

所以我有一個類,它看起來像這樣:WPF:無法獲取自定義附加屬性的觸發物業工作

internal class MyClass 
{ 
    public static readonly DependencyProperty IsSomethingProperty = 
      DependencyProperty.RegisterAttached(
       "IsSomething", // property name 
       typeof(bool), // property type 
       typeof(MyClass), // owner type 
       new FrameworkPropertyMetadata(false) 
       ); 

    public static void SetIsSomething(DependencyObject obj, bool value) 
    { 
     obj.SetValue(IsSomethingProperty, value); 
    } 

    [AttachedPropertyBrowsableForType(typeof(TreeViewItem))]   
    public static bool GetIsSomething(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(IsSomethingProperty); 
    } 
} 

我希望能夠利用這個附加屬性作爲一個觸發屬性控制模板,像這樣:

<ControlTemplate TargetType="TreeViewItem"> 
    <!-- stuff here omitted for brevity --> 
    <Trigger Property="my:MyClass.IsSomething" Value="True"> 
     <!-- setters for when IsSomething is True --> 
    </Trigger> 
</ControlTemplate> 

(上面的控制模板假定適當xmlns:my="clr-namespace:MyAssembly",其中包含MyAssembly的MyClass的是在封閉的XAML文件)

這裏的我的麻煩:當我這樣做時,它編譯得很好。但是,當我嘗試在設計器中看到此控件模板時,它會投訴Cannot find the 'IsSomething' template property on type 'System.Windows.Controls.TreeViewItem'.,並且設計器將無法加載。

我試過RegisterAttached覆蓋MyClass以及TreeViewItem作爲所有者類型,都沒有修復此問題。我也嘗試過在GetIsSomething上使用AttachedPropertyBrowsableForType屬性。有沒有人看到問題是什麼?

回答

4

只要我發佈問題就找到了答案。我發佈的答案可以幫助遇到同樣問題的任何人。 標記你的課程公開。不知道這是否是有意設計的,但看起來他們至少可以改善這裏的錯誤信息。

希望這可以幫助別人。

0

通過在項目的屬性中設置「啓動對象」可以魔法般地修復這些東西。