【问题标题】:Cannot convert the value in attribute '[attr]' to object of type 'System.Windows.TemplateBindingExtension'无法将属性“[attr]”中的值转换为“System.Windows.TemplateBindingExtension”类型的对象
【发布时间】:2010-04-09 14:02:43
【问题描述】:

当我在类层次结构之外的类中定义附加的依赖项属性并将所有者设置为公共父类时,出现此错误。

WindowBase 类中的附加依赖属性(在类层次结构之外 => 生成错误):

public static readonly DependencyProperty AreaColorProperty =
DependencyProperty.RegisterAttached("AreaColor", typeof(AreaColor), typeof(Window));

模板绑​​定失败

{TemplateBinding local:WindowBase.AreaColor}

如果我改为在类层次结构中的类中定义附加的依赖属性并将所有者设置为该类,那么我不会收到任何错误,这是为什么呢?

WindowBase 中的附加依赖属性(在类层次结构内 => 没有错误):

public static readonly DependencyProperty AreaColorProperty =
DependencyProperty.RegisterAttached("AreaColor", typeof(AreaColor), typeof(WindowBase));

最好的问候, 杰斯珀

【问题讨论】:

  • 当然问题是 TemplateBinding 应该是:{TemplateBinding Window.AreaColor}。虽然这产生了一个新错误:在类型 Window 上找不到静态成员“AreaColorProperty”。然后我可以通过在 TemplateBinding 上指定一个转换器来解决这个问题...嗯

标签: wpf attached-properties templatebinding


【解决方案1】:

我通过切换到相对源绑定解决了这个问题:

{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=(Window.CaseAreaColor)}

【讨论】:

  • 这也可以作为使用属性和转换器解决模板绑定损坏的解决方案,否则不会抛出“无法将属性'Property'中的值转换为'System.Windows.DependencyProperty'类型的对象” ' 如果不使用转换器,则会出错。
猜你喜欢
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 2017-09-08
  • 2021-06-13
  • 2021-07-08
  • 1970-01-01
  • 2017-05-06
  • 2015-03-29
相关资源
最近更新 更多