WPF: unable to get custom attached property to work as trigger property

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 nested property as a trigger property in a control template, for example:

<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 correct one xmlns:my="clr-namespace:MyAssembly", where MyAssembly contains MyClass in the attached XAML file)

Here is my problem: when I do this, it compiles fine. However, when I try to see this control pattern in action in the constructor, it complains about Cannot find the 'IsSomething' template property on type 'System.Windows.Controls.TreeViewItem'., and the constructor does not load.

RegisterAttached MyClass, TreeViewItem , . AttachedPropertyBrowsableForType <<28 > . - , ?

+3
2

, . , , . . , , , , , , .

, -.

+4

- " " .

0

Source: https://habr.com/ru/post/1752289/


All Articles