Wpf 在样式设置器中设置自定义附加属性

Wpf 在样式设置器中设置自定义附加属性,wpf,styles,attached-properties,Wpf,Styles,Attached Properties,我试图在样式内设置附加属性,我想使用它们来附加行为。但是我不能让它工作。 这里是代码: 附属财产 public class TestBehaviour { public static bool GetTest(Grid grid) { return (bool)grid.GetValue(TestProperty); } public static void SetTest(Grid grid, bool value) {

我试图在样式内设置附加属性,我想使用它们来附加行为。但是我不能让它工作。 这里是代码:

附属财产

public class TestBehaviour
{
    public static bool GetTest(Grid grid)
    {
        return (bool)grid.GetValue(TestProperty);
    }

    public static void SetTest(Grid grid, bool value)
    {
        grid.SetValue(TestProperty, value);
    }

    public static readonly DependencyProperty TestProperty = DependencyProperty.RegisterAttached("Test", typeof(bool), typeof(Grid));

}
Xaml


附加属性的所有者类型必须是声明它的类,这里是TestBehavior,而不是Grid。将声明更改为:

public static readonly DependencyProperty TestProperty =
    DependencyProperty.RegisterAttached("Test", typeof(bool), typeof(TestBehaviour));
有关以下信息,请参阅MSDN文档:

ownerType—正在注册依赖项属性的所有者类型


你试过括号测试吗?@Pragmateek-tryied刚刚。不会编译测试未声明的命名空间也尝试了TestBehaviorOut.test
public static readonly DependencyProperty TestProperty =
    DependencyProperty.RegisterAttached("Test", typeof(bool), typeof(TestBehaviour));