为什么这个属性不可见?说找不到可附加的属性

发布于 2024-12-15 05:58:43 字数 1393 浏览 1 评论 0原文

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

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 命名空间中的“网格” 'clr-命名空间:基础设施.行为;程序集=基础设施.SL'。 C:\MainPage.xaml 11 11 Controls.SL.Test

错误 1 ​​未找到可附加属性“InteractionsProvider” 类型 “交互行为”。 C:\MainPage.xaml 11 11 Controls.SL.Test

I'm creating Behavior with attached properties. Behavior should attach to Grid:

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

Now when I'm writing XAML like this I get error:

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

Error 4 The property 'InteractionsProvider' does not exist on the type
'Grid' in the XML namespace
'clr-namespace:Infrastructure.Behaviors;assembly=Infrastructure.SL'. C:\MainPage.xaml 11 11 Controls.SL.Test

Error 1 The attachable property 'InteractionsProvider' was not found
in type
'InteractionsBehavior'. C:\MainPage.xaml 11 11 Controls.SL.Test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-12-22 05:58:43

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

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

或使用 Grid 的类层次结构中的某个基类...)

You specified that it only should be available to be attached ("owned") by an InteractionsBehavior. If you want to be able to assign this to a grid, change the RegisterAttached line to:

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

(Or use some base class in Grid's class hierarchy...)

青衫负雪 2024-12-22 05:58:43

问题出在你的附加财产的声明上。附加属性有 4 部分:名称、类型、所有者类型和属性元数据。您指定 InteractionsProvider 属性由 Grid 类型拥有(并因此提供)。实际情况并非如此。将所有者类型(第三个参数)更改为 typeof(InteractionsBehavior) (您在其中声明附加属性的类),切换到静态 get/set 方法而不是属性(因为您是使用附加属性,而不是依赖属性),并且它应该全部按您的预期工作。

The problem is in the declaration of your attached property. Attached properties have 4 parts: the name, the type, the owner type, and the property metadata. You're specifying that the InteractionsProvider property is owned (and thus supplied) by the type Grid. That's not actually the case. Change the owner type (third parameter) to typeof(InteractionsBehavior) (the class in which you've declared the attached property), switch to static get/set methods instead of a property (because you're using an attached property, not a dependency property), and it should all work as you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文