【问题标题】:Tree Grid View binding errors on collection change集合更改时的树网格视图绑定错误
【发布时间】:2012-02-16 08:12:36
【问题描述】:

我有一个 WPF MVVM 应用程序,其中包含一个自定义树网格视图,该视图从可观察集合中获取其信息。

如果我运行应用程序而不更改可观察集合中的任何内容,它运行良好。

但是,如果我更改可观察集合中的项目,我会收到以下错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'TreeListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 如何更改集合的内容?
  • 它正在被一个定时器事件改变。在事件调用中,我有另一个集合,我循环并仅将有效项目添加到显示的列表中。

标签: c# wpf treeview observablecollection


【解决方案1】:

根据这个very short blog post,当您尝试绑定ListBoxItems 时可能会发生此错误。现在在上面的错误中,您在使用 TreeListViewItem 的目标元素时遇到了问题,但我绝对可以看到这可能是同一个问题。您可以尝试博客建议的两种解决方法之一吗?两者都只是为 TreeListViewItem(或者在他的情况下为 ListBoxItem)创建自定义样式。这两个建议是:

  1. 显式设置 Horizo​​ntalContentAlignment 和 VerticalContentAlignment 属性
  2. 将 OverridesDefaultStyle 设置为 true 并忽略该值

    <Style x:Key="DeviceContainerStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    

或者:

    <Style x:Key="DeviceContainerStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="OverridesDefaultStyle" Value="True"/>

当然,您可以将这些更改为TreeListViewItem 而不是ListBoxItem。 =)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2013-09-14
    相关资源
    最近更新 更多