许多意外的“无法使用绑定检索值”错误

时间:2018-08-14 07:40:53

标签: wpf data-binding datagrid wpfdatagrid

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

  

System.Windows.Data信息:10:无法使用绑定来检索值,并且不存在有效的回退值;请参见使用默认值代替。 BindingExpression:Path = AreRowDetailsFrozen; DataItem = null;目标元素是'DataGridDetailsPresenter'(Name ='');目标属性是“ SelectiveScrollingOrientation”(类型为“ SelectiveScrollingOrientation”)

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

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

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

我担心这些“绑定错误”会减慢我的实际应用程序的速度(在该应用程序中我的集合中可能有超过5万个元素),因此想了解它们为什么会出现以及如何避免它们 >。

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

1 个答案:

答案 0 :(得分:2)

这些绑定错误是无害的,除修改构成DataGrid的元素的默认模板外,您无需采取其他任何措施即可消除它们,这不仅需要付出很大的努力,而且可能还会让您失去一些控件的内置功能。

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

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