3

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

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>

Code behind:

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; }
}

The two elements are displayed correctly in the DataGrid.

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

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.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • I can't reproduce the issue. Are ID and Text - properties? – Rekshino Aug 14 '18 at 08:02
  • @Rekshino yes they are properties, I'll add this little class to the question. To reproduce you must debug the app and watch the output window, maybe you have different trace settings so that the messages are not logged (trace settings are described [here](https://stackoverflow.com/a/11540137/5528593)) – René Vogt Aug 14 '18 at 08:05
  • 1
    If I set trace level to Verbose, then I can see the entries. The question is - should you worry about them? – Rekshino Aug 14 '18 at 08:12
  • @Rekshino It seems the messages appear because the rows are set up before inserting them into the visual tree, so initially their datacontext is null and they get their values only on second try...I'm not sure if I should really worry about that, but what I've read so far says these kind of errors do have a performance impact (not sure if they were all internal expensive excpetions). I'm still developing my app, so I don't have final performance data but already get the feeling that my datagrid becomes slow. (btw: thanks for taking the time to reproduce). – René Vogt Aug 14 '18 at 08:19
  • related: https://stackoverflow.com/questions/3846823/getting-many-binding-information-in-wpf-output-window – René Vogt Aug 14 '18 at 08:22
  • I doubt about adding the columns with no context, because you have 6 trace outputs per data item, not always only 6 trace outputs. So DataContext with data items must be set. – Rekshino Aug 14 '18 at 08:27
  • Of course there(in DataGrid implementation) can be a bug, but what we can do? Do not use DataGrid, use UserControl? – Rekshino Aug 14 '18 at 08:29
  • @Rekshino I'm not sure, maybe I misunderstood what I've read or confused it (did a lot of googling...). I tend to think you're right: these are only binding _information_, not _errors_, so they probably don't have the mentioned performance impact. I'll analyze this further and come back to this question when I have new information (or delete it if it's really nothing to worry about). Thanks for you effort. – René Vogt Aug 14 '18 at 08:34

1 Answers1

2

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.

Resolving harmless binding errors in WPF: https://weblogs.asp.net/akjoshi/resolving-un-harmful-binding-errors-in-wpf

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    These errors may impact your performane in Debug mode. In VS2015 you can disable them: Tools->Options->Debugging->Output Window WPF Trace Settings , set to Off whatever is bothering you. – Mihai Sep 24 '20 at 09:10
  • Just want to add to @Mihai's suggestion that setting Tools->Options->Debugging->Output Window WPF Trace Settings->Data Binding to "Error" instead of "Off" will remove the massive amount of errors in the output window, making your debug session faster without loosing the ability to see binding errors in the XAML debugger (black row in windows when debugging). – mottosson Oct 29 '21 at 08:58