【问题标题】:Lots of unexpected "Cannot retrieve value using the binding" errors许多意外的“无法使用绑定检索值”错误
【发布时间】:2018-08-14 07:40:52
【问题描述】:

在调试我的 wpf 项目时,我看到在输出窗口中记录了很多绑定错误,如下所示:

System.Windows.Data 信息:10:无法使用绑定检索值,并且不存在有效的备用值;改为使用默认值。绑定表达式:路径=AreRowDetailsFrozen;数据项=空;目标元素是'DataGridDetailsPresenter'(名称='');目标属性是“SelectiveScrollingOrientation”(输入“SelectiveScrollingOrientation”)

我在 Google 上搜索了很多关于此类消息的信息,并尝试修复所有 我的 绑定,但我从未听说过的属性不断出现错误。

所以我将其分解为一个基本示例:

xaml:

<StackPanel>
    <DataGrid ItemsSource="{Binding Items}" x:Name="_grid" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding ID, FallbackValue=0}"/>
            <DataGridTextColumn Header="Text" Binding="{Binding Text, FallbackValue={x:Null}}"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button Click="Button_OnClick">Reset</Button>
</StackPanel>

后面的代码:

public partial class MainWindow
{
    public ObservableCollection<TestItem> Items { get; } = new ObservableCollection<TestItem>();    
    public MainWindow()
    {
        Items.Add(new TestItem { ID = 1, Text = "One" });
        Items.Add(new TestItem { ID = 2, Text = "Two" });
        InitializeComponent();
    }
    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        _grid.ItemsSource = null;
        _grid.ItemsSource = Items;
    }
}

public class TestItem
{
    public int ID { get; set; }
    public string Text { get; set; }
}

这两个元素在DataGrid中正确显示。

现在,每当我单击按钮(并重新分配 ItemSource)时,我都会在输出窗口中看到这 12 条消息:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ID; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Text; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ValidationErrorTemplate; DataItem=null; target element is 'Control' (Name=''); target property is 'Template' (type 'ControlTemplate')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=(0); DataItem=null; target element is 'Control' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ID; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Text; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ValidationErrorTemplate; DataItem=null; target element is 'Control' (Name=''); target property is 'Template' (type 'ControlTemplate')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=(0); DataItem=null; target element is 'Control' (Name=''); target property is 'Visibility' (type 'Visibility')

我检查了将ItemSource 设置回Items 时出现的错误,而不是设置为null 时出现的错误。并且错误消息的数量取决于集合中的项目数。

我担心这些“绑定错误”会减慢我的实际应用程序(我的集合中可能有 >50k 元素),因此想了解它们出现的原因以及如何避免它们.

如您所见,我已经在绑定中添加了备用值,但是对于我根本没有绑定的属性,错误仍然出现。

【问题讨论】:

  • 我无法重现该问题。是 ID 和 Text - 属性吗?
  • @Rekshino 是的,它们是属性,我会将这个小类添加到问题中。要重现,您必须调试应用程序并观察输出窗口,也许您有不同的跟踪设置,以便不记录消息(跟踪设置描述为here
  • 如果我将跟踪级别设置为详细,那么我可以看到条目。问题是——你应该担心他们吗?
  • @Rekshino 似乎出现消息是因为行是在将它们插入可视化树之前设置的,所以最初它们的 datacontext 为空,它们仅在第二次尝试时才获得它们的值......我不确定我是否真的应该担心这一点,但到目前为止我所读到的内容表明这些错误确实会对性能产生影响(不确定它们是否都是内部昂贵的异常)。我仍在开发我的应用程序,所以我没有最终的性能数据,但已经感觉到我的数据网格变慢了。 (顺便说一句:感谢您花时间复制)。

标签: wpf data-binding datagrid wpfdatagrid


【解决方案1】:

这些绑定错误是无害的,除了修改构成 DataGrid 的元素的默认模板之外,您无法摆脱它们,这不仅需要相当大的努力,而且可能会丢失你控件的一些内置功能。

您显然应该避免您自己负责的绑定错误,但是当涉及到您从框架“继承”而没有自定义任何模板的绑定错误时,您可以放心地忽略它们。这些绑定错误中的大多数都是无害的,并且已经在内部进行了处理。因此,只需忽略它们,什么也不做或压制它们。请参考以下链接了解更多信息。

解决 WPF 中的无害绑定错误: https://weblogs.asp.net/akjoshi/resolving-un-harmful-binding-errors-in-wpf

【讨论】:

  • 这些错误可能会影响您在调试模式下的性能。在 VS2015 中你可以禁用它们: Tools->Options->Debugging->Output Window WPF Trace Settings ,设置为 Off 任何困扰你的东西。
  • 只是想补充@Mihai的建议,将工具->选项->调试->输出窗口WPF跟踪设置->数据绑定设置为“错误”而不是“关闭”将删除大量输出窗口中的错误,使您的调试会话更快,而不会失去在 XAML 调试器中查看绑定错误的能力(调试时窗口中的黑行)。
猜你喜欢
  • 1970-01-01
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-12
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多