C# 为什么这个属性不是';看不见?表示可附加属性可以';找不到

C# 为什么这个属性不是';看不见?表示可附加属性可以';找不到,c#,silverlight,xaml,behavior,C#,Silverlight,Xaml,Behavior,我正在创建带有附加属性的行为。行为应附加到网格: public class InteractionsBehavior : Behavior<Grid> { public static readonly DependencyProperty ContainerProperty = DependencyProperty.RegisterAttached("Container", typeof(Grid), typeof(Grid), new P

我正在创建带有附加属性的行为。行为应附加到网格:

public class InteractionsBehavior : Behavior<Grid>
{
    public static readonly DependencyProperty ContainerProperty =
        DependencyProperty.RegisterAttached("Container", typeof(Grid), typeof(Grid), new PropertyMetadata(null));

    public static readonly DependencyProperty InteractionsProviderProperty =
        DependencyProperty.RegisterAttached("InteractionsProvider", typeof(IInteractionsProvider), typeof(Grid), new PropertyMetadata(null, OnInteractionsProviderPropertyChanged));

    public Grid Container
    {
        get { return GetValue(ContainerProperty) as Grid; }
        set { this.SetValue(ContainerProperty, value); }
    }

    public IInteractionsProvider InteractionsProvider
    {
        get { return GetValue(InteractionsProviderProperty) as IInteractionsProvider; }
        set { this.SetValue(InteractionsProviderProperty, value); }
    }
公共类交互行为:行为
{
公共静态只读从属属性ContainerProperty=
DependencyProperty.RegisterAttached(“容器”、typeof(网格)、typeof(网格)、new PropertyMetadata(null));
公共静态只读从属属性InteractionProviderProperty=
DependencyProperty.RegisterAttached(“InteractionProvider”、typeof(IIInteractionsProvider)、typeof(Grid)、new PropertyMetadata(null、OnInteractionsProviderPropertyChanged));
公共网格容器
{
获取{返回GetValue(ContainerProperty)作为网格;}
set{this.SetValue(ContainerProperty,value);}
}
公共交互提供程序交互提供程序
{
获取{返回GetValue(InteractionProviderProperty)作为IInteractionsProvider;}
set{this.SetValue(InteractionProviderProperty,value);}
}
现在,当我像这样编写XAML时,我得到一个错误:

<Grid Background="White" x:Name="LayoutRoot" 
          Behaviors:InteractionsBehavior.InteractionsProvider="{Binding InteractionsProvider}">

错误4类型上不存在属性“InteractionProvider” XML命名空间中的“网格” “clr命名空间:Infrastructure.Behaviors;assembly=Infrastructure.SL'.C:\MainPage.xaml 11 Controls.SL.Test”

错误1未找到可附加属性“InteractionProvider” 打字 “InteractionBehavior”.C:\MainPage.xaml 11 Controls.SL.Test


您指定它只能由
InteractionBehavior
附加(“拥有”)。如果希望能够将其分配给网格,请将RegisterAttached行更改为:

public static readonly DependencyProperty InteractionsProviderProperty =
        DependencyProperty.RegisterAttached("InteractionsProvider", typeof(IInteractionsProvider), typeof(Grid), new PropertyMetadata(null, OnInteractionsProviderPropertyChanged));

(或者在
网格中使用一些基类
的类层次结构…

问题在于附加属性的声明。附加属性有4个部分:名称、类型、所有者类型和属性元数据。您指定InteractionProvider属性为owned(并因此提供)按类型网格。事实并非如此。将所有者类型(第三个参数)更改为
typeof(InteractionBehavior)
(声明附加属性的类),切换到静态get/set方法,而不是属性(因为您使用的是附加属性,而不是依赖属性),它应该可以像您期望的那样工作。

我刚刚编辑了我的帖子-这是同一个问题-我得到了相同的错误。我想知道类本身是否有问题?行为是什么意思?@katit这是一种表达式行为-如果您使用它,您可能不需要附加属性。您通常会使用行为或附加属性。您可以如果要使用附加属性,还需要修改get/set访问器的规范-请参阅:我不知道有不同的行为类型。现在,表达式行为似乎更适合我,因为它似乎有“state”.您回答了我的问题,如果您有关于如何编写表达式的好教程,我将不胜感激behavior@katit试试看:迈克,我标记了里德的答案。事实上,当我按照命名约定添加SetInteractionProvider和GetInteractionProvider时,它就开始工作了。但是,“静态”这段代码的性质似乎不适合我,我需要探索表达式行为,请尝试以下链接: