如何调试 wpfrelativesource 在加载时抛出错误?

发布于 2024-07-12 01:37:36 字数 969 浏览 14 评论 0原文

我正在使用包含 ListBox 的 WPF 对话框窗口。 列表框可以显示“图标”和“列表”视图。 为了实现此目的,我们使用了动态应用于 ListBoxListBoxItem 样式,以及一对相应的 ItemsPanelTemplate。 照原样,它显示得很漂亮。 但是,加载此对话框时,我们会遇到几十个绑定错误,如下所示:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl' , AncestorLevel='1''。 BindingExpression:Path=VerticalContentAlignment; 数据项=空; 目标元素是“ListBoxItem”(名称=“”); 目标属性是“VerticalContentAlignment”(类型“VerticalAlignment”)

我们的理论是样式和模板在 ListBox 之前加载,并且无法使用以下代码找到它们绑定到的属性:

<Setter Property="VerticalContentAlignment" Value="{Binding 
        Path=VerticalContentAlignment, RelativeSource={RelativeSource 
        AncestorType={x:Type ItemsControl}}}"/> 

这似乎在 ListBox 初始化并应用样式和模板之前发生一次或多次。 对话框完成初始化后,错误就会停止。

以前有人遇到过这种情况吗? 谁能提出一个我们可能忽略的解决方案?

I'm working with a WPF dialog window that contains a ListBox. The list box can display both an 'icon' and 'list' view. To accomplish this, we used ListBoxItem styles that are dynamically applied to the ListBox, as well as a pair of corresponding ItemsPanelTemplates. As is, it displays beautifully. However, when loading this dialog we get a few dozen binding errors, such as the following:

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

Our theory is that the styles and templates are loading before the ListBox, and failing to find the properties they are bound to using code such as:

<Setter Property="VerticalContentAlignment" Value="{Binding 
        Path=VerticalContentAlignment, RelativeSource={RelativeSource 
        AncestorType={x:Type ItemsControl}}}"/> 

This seems to happen one or more times before the ListBox is initialized and the styles and templates are applied. After the dialog finishes initializing, the errors stop.

Has anyone encountered this before? Can anyone suggest a solution we may have overlooked?

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

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

发布评论

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

评论(2

酒与心事 2024-07-19 01:37:36

看起来这里已经涵盖了:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/db050ce6-d084-41ad-9a31-c01831687683

应该是一个“已知”问题。

Looks like this was covered here:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/db050ce6-d084-41ad-9a31-c01831687683

Supposed to be a "known" issue.

四叶草在未来唯美盛开 2024-07-19 01:37:36

我在这里找到了修复:

http://blog.jtango.net/2009/05/08/using-a-grid-as-the-itemspanel-for-a-listbox-in-wpf-binding -errors/

您使用 ListBox 上的 ItemContainerStyle 设置样式。

I found a fix here:

http://blog.jtango.net/2009/05/08/using-a-grid-as-the-itemspanel-for-a-listbox-in-wpf-binding-errors/

You set the style using ItemContainerStyle on the ListBox.

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