WPF:无法将自定义附加属性作为触发器属性使用

4

我有一个类长这样:

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"在包含XAML文件中,MyAssembly包含MyClass。
我的问题是:当我这样做时,它可以编译。 但是,当我尝试在设计器中查看此控件模板时,它会抱怨无法在类型“System.Windows.Controls.TreeViewItem”上找到“IsSomething”模板属性。并且设计器无法加载。
我已经尝试使用MyClass和TreeViewItem作为所有者类型来覆盖RegisterAttached,但两者都没有解决这个问题。 我也尝试了在GetIsSomething上使用AttachedPropertyBrowsableForType属性和不使用该属性。 有人看到问题了吗?
2个回答

4

我刚发布问题,就立刻找到了答案。我现在发布答案,帮助那些遇到同样问题的人。把你的类标记为public。不确定这是不是设计上的问题,但至少他们可以在这里改进错误信息。

希望这能帮助到某个人。


0

通过在项目属性中设置“启动对象”,像这样的问题可以神奇地得到解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接