集合更改时的树网格视图绑定错误

发布于 2025-01-05 17:52:51 字数 555 浏览 1 评论 0 原文

我有一个 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')

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

I have a WPF MVVM application that contains a custom tree grid view that gets its information from an observable collection.

If I run the application without changing anything in the observable collection, it runs fine.

However if I change items within the observable collection I get the following error:

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')

Any ideas as to how I would solve this?

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

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

发布评论

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

评论(1

尹雨沫 2025-01-12 17:52:51

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

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

    <样式 x:Key="DeviceContainerStyle" TargetType="{x:Type ListBoxItem}">
    
    
    

或者:

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

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

According to this very short blog post this error can happen when you try to bind ListBoxItems. Now in your error above, you are having trouble with a target element of TreeListViewItem but I could definitely see how this could be the same problem. Could you try one of the two methods for fixing this that the blog suggests? Both are simply creating a custom style for the TreeListViewItem (or in his case a ListBoxItem). The two suggestions are to:

  1. Explicitly set the HorizontalContentAlignment and VerticalContentAlignment property
  2. Set OverridesDefaultStyle to true and just ignore the value

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

Or:

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

You would, of course, change these for TreeListViewItem not for ListBoxItem. =)

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