【发布时间】:2011-02-06 06:37:56
【问题描述】:
我正在处理一个输出大量绑定错误的大型 WPF 应用程序。一个典型的错误如下所示:
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 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
问题是我不知道这是来自应用程序的哪个位置。搜索 AncestorType={x:Type ItemsControl} 的整个解决方案并没有必要的帮助,因为我仍然不知道哪个结果是罪魁祸首。我试过设置PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.All;
但额外的信息无助于定位有问题的绑定。文件名和行号是我真正需要的。有没有办法得到这些信息?我能想到的唯一其他解决方案是为候选者分配名称,然后从那里缩小范围。在这种情况下,有没有办法为整个解决方案中的所有可命名元素自动分配一个唯一的 x:Name?
【问题讨论】:
标签: wpf visual-studio data-binding binding