【问题标题】:How to set Dependency Properties in a Control which inherits from an abstract UserControl?如何在从抽象 UserControl 继承的控件中设置依赖属性?
【发布时间】:2012-02-01 00:44:42
【问题描述】:

我有一个 Control (mySubControl),它继承自一个抽象的 UserControl (myAbstractControl)。 当我尝试在 mySubControl 中设置依赖属性时,我总是在运行时出错,说:

“System.Windows.Data 错误:40:BindingExpression 路径错误: 'myPropertyRegistrationName' 属性在....上找不到。”

我认为 mySubControl 的 DataContext 不知何故搞砸了。

我尝试使用相对绑定,但它也无法修复它。

Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type myNS:myType}},
                     Path=myPropertyRegistrationName}"

谁能帮帮我?

谢谢

【问题讨论】:

  • not found on 的部分很重要,... 没有帮助。如果DataContext 不是您想要的,那部分会告诉您。
  • 什么是myNS:myType?还考虑到您正在绑定到 Type 实例,您的属性 myPropertyRegistrationName 应该声明为静态并且也应该是 Visibility 类型,因为我在绑定中也没有看到任何转换器。跨度>
  • 请使用actula DP 声明更新您的帖子。确保使用正确的名称(即 DP 声明中的第一个参数),而不是 DP 属性的实际 CLR 名称。

标签: c# wpf inheritance dependency-properties relativesource


【解决方案1】:

总是语法,不是吗?

Visibility="{Binding myPropertyRegistrationName, RelativeSource={RelativeSource FindAncestor, AncestorType=myNS:myType}}"

无论如何谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-08
    • 2022-12-17
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多