大量意外的“无法使用绑定获取值”错误 [英] Lots of unexpected "Cannot retrieve value using the binding" errors

查看:93
本文介绍了大量意外的“无法使用绑定获取值”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试wpf项目时,我在输出窗口中看到很多这样的绑定错误:

When debugging my wpf project I see a lot of binding errors logged in the output window like this one:


System.Windows。数据信息:10:无法使用绑定检索值,并且不存在有效的后备值;使用默认值代替。 BindingExpression:Path = AreRowDetailsFrozen; DataItem = null;目标元素是 DataGridDetailsPresenter(名称=);目标属性是 SelectiveScrollingOrientation(类型为 SelectiveScrollingOrientation)

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

我在此类消息中搜索了很多,并试图修复所有我的绑定,但是对于我什至从未听说过的属性,错误仍然不断发生。

I googled a lot about this kind of message and tried to fix all my bindings, but the errors keep occuring for properties I never even heard about.

因此,我将其分解为一个基本示例:

So I broke this down to a basic example:

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条消息:

Now whenever I click the button (and reassign the ItemSource) I see these 12 messages in the output window:

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 时。错误消息的数量取决于集合中项目的数量。

I checked that the errors appear when setting ItemSource back to Items, not when setting to null. And the number of error messages depends on the number of items in the collection.

我担心这些绑定错误会使我的实际应用程序变慢(在这种情况下, 。

I am concerned that these "binding errors" will slow down my real application (where I may have >50k elements in the collection) and therefor want to understand why they appear and how I can avoid them.

如您所见,我想要了解它们为什么会出现以及如何避免它们。已经向我的绑定添加了后备值,但是对于我根本没有绑定的属性,仍然会出现错误。

As you can see I already added fallback values to my bindings, but the errors keep appearing for properties I didn't bind at all.

推荐答案

这些绑定错误是无害的,除修改构成 DataGrid 的元素的默认模板外,您无需采取任何措施即可消除这些错误,而且不仅需要努力,但它也可能使您失去控件的某些内置功能。

These binding errors are harmless and it's not much you can do to get rid of them besides modifying the default templates of the elements that make up the DataGrid, and that would not only require quite an effort but it may also lose you some of the built-in functionality of the control.

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

You should obviously avoid binding errors that you are responsible for yourself but when it comes to binding errors that you "inherit" from the framework without customizing any templates, you can safely just ignore them. Most of these binding errors are both harmless and already handled internally. So just ignore them and do nothing or suppress them. Please refer to the following link for more information.

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

这篇关于大量意外的“无法使用绑定获取值”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆