如何调试 wpfrelativesource 在加载时抛出错误?
我正在使用包含 ListBox
的 WPF 对话框窗口。 列表框可以显示“图标”和“列表”视图。 为了实现此目的,我们使用了动态应用于 ListBox
的 ListBoxItem
样式,以及一对相应的 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 ItemsPanelTemplate
s. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来这里已经涵盖了:
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.
我在这里找到了修复:
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 theListBox
.