代码之家  ›  专栏  ›  技术社区  ›  René Vogt

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

  •  2
  • René Vogt  · 技术社区  · 6 年前

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

    System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;请改用默认值。bindingExpression:path=arerowdetailsFrozed;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')
    

    我检查了设置时出现的错误 项目源 返回 Items ,设置为时不显示 null . 错误消息的数量取决于集合中的项数。

    我担心这些“绑定错误”会减慢我的实际应用程序的速度(我可能在集合中有50k个元素),因此 想知道他们为什么出现以及我如何避免他们 .

    正如您所看到的,我已经向绑定中添加了回退值,但是对于我根本没有绑定的属性,错误仍然会出现。

    1 回复  |  直到 6 年前
        1
  •  2
  •   mm8    6 年前

    这些绑定错误是无害的,除了修改组成元素的默认模板外,您不需要做太多的工作来消除它们。 DataGrid ,这不仅需要付出很大的努力,而且可能会丢失控件的一些内置功能。

    显然,您应该避免自己负责的绑定错误,但是当您在不自定义任何模板的情况下从框架“继承”绑定错误时,您可以安全地忽略它们。大多数这些绑定错误都是无害的,并且已经在内部处理过。所以忽略它们,什么也不做或者抑制它们。有关详细信息,请参阅以下链接。

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