为什么此属性不是';不可见?说可扣押财产可以;找不到

本文关键字:财产 找不到 为什么 属性 | 更新日期: 2023-09-27 18:14:51

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

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

现在,当我像这样写XAML时,我会得到错误:

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

错误4类型上不存在属性"InteractionsProvider"XML命名空间中的"Grid"'clr命名空间:Infrastructure.Behaviors;assembly=Infrastructure.SL'.C:''MainPage.xaml 11 11 Controls.SL.Test

错误1找不到可附加的属性"InteractionsProvider"in type"交互行为"。C: ''MainPage.xaml 11 11 Controls.SL测试

为什么此属性不是';不可见?说可扣押财产可以;找不到

您指定它只能由InteractionsBehavior附加("拥有"(。如果您想将其分配给网格,请将RegisterAttached行更改为:

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

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

问题在于附加属性的声明。附加的属性有4个部分:名称、类型、所有者类型和属性元数据。您正在指定InteractionsProvider属性由类型Grid拥有(并因此提供(。事实并非如此。将所有者类型(第三个参数(更改为typeof(InteractionsBehavior)(您在其中声明了附加属性的类(,切换到静态获取/设置方法而不是属性(因为您使用的是附加属性,而不是依赖属性(,所有这些都应该如您所期望的那样工作。