开发者

WPF: Cannot Get Custom Attached Property to work as a Trigger Property

开发者 https://www.devze.com 2023-01-05 06:52 出处:网络
So I have a class that looks like this: internal class MyClass { public static readonly DependencyProperty IsSomethingProperty =

So I have a class that looks like this:

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

I would like to be able to use this attached property as a trigger property in a control template, like so:

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

(the above control template assumes the proper xmlns:my="clr-namespace:MyAssembly" where MyAssembly contains MyClass is in the enclosi开发者_运维问答ng XAML file)

Here's my trouble: when I do this, it compiles fine. However, when I try to see this control template in action in the designer, it complains Cannot find the 'IsSomething' template property on type 'System.Windows.Controls.TreeViewItem'. and the designer won't load.

I've tried the RegisterAttached override with MyClass as well as TreeViewItem as the owner type, neither fixes this. I've also tried it with and without the AttachedPropertyBrowsableForType attribute on GetIsSomething. Does anyone see what the problem is?


Found the answer as soon as I posted the question. I'm posting the answer to help anyone who's run into the same issue. Mark your class public. Not sure if this is by design or not, but it seems they could at least improve the error message here.

Hope this helps someone.


Had something like this magically fixed by setting the "startup object" in the project's properties.

0

精彩评论

暂无评论...
验证码 换一张
取 消