Resolving harmless binding errors in WPF

While developing WPF applications, you will notice a lot of binding errors being displayed in output window; like this

System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', 
AncestorLevel='1''. BindingExpression:Path=CellsPanelHorizontalOffset; DataItem=null; 
target element is 'Button' (Name=''); target property is 'Width' (type 'Double')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', 
AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; 
target element is 'ComboBoxItem' (Name=''); target property is 
'HorizontalContentAlignment' (type 'HorizontalAlignment')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', 
AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; 
target element is 'ComboBoxItem' (Name=''); target property is 
'VerticalContentAlignment' (type 'VerticalAlignment')

I also faced this problem and tried a lot of things to get to the root cause of the problem. It was very frustrating as AncestorLevel is not used anywhere in code! and I was not able to find the place in code which is responsible for these errors.

Even after searching the various forums and articles there was no solution for this problem; but this was a very common issue and cause of this problem as mentioned on various forums:

This is a "known" issue, and happens to all controls that contain dynamically created lists (all item controls i.e. ComboBox, menu, ListBox etc.).
ControlTemplate of items in these controls (specifically MenuItem, ComboBoxItem etc.)
try to find the nearest ItemsControl and bind to the VerticalAlignment and HorizonalAlignment properties and raises this error on not finding the source.

Microsoft guys mention here and here that “This error has already been handled internally, so you can just leave it alone.” But, still I wanted some sort of solution so as not to have so many irritating error messages in my output window;

Solution 1:
 
So, I tried one of the workaround provided for this problem i.e. to explicitly set the properties which cause problems like this -
<ComboBox
    Name="control">
    <ComboBox.ItemContainerStyle>
        <Style
            TargetType="ComboBoxItem">
            <Setter
                Property="HorizontalContentAlignment"
                Value="Left" />
            <Setter
                Property="VerticalContentAlignment"
                Value="Center" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
This works but it’s very hard task to set these properties across the whole solution for each problematic control(i.e. ListBox, Menu, ContextMenus etc.).
Have a look at this SO question for some other workarounds for this problem - ListBox with Grid as ItemsPanelTemplate produces weird binding errors
 
Solution 2:
 
Another workaround is to suppress these errors (actually, it seems more appropriate to call them warnings) by setting the data binding source switch level as critical in constructor of the class or a top level window -
#if DEBUG 
    System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;
#endif

I find this one line workaround more appropriate as it works for all such controls (like Menus, ListBox etc.) and that too across the whole project  -

Ref.: How to suppress the System.Windows.Data Error warning message

 

5 Comments

  • I recommend using:

    System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Error;

    because if you use:

    System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;

    it not only eliminates the benign error discussed above but also does not report the "real" error of specifying a non-existant source path. This is not a problem with if you use "SourceLevels.Error"

  • Oops! I blew it. SourceLevels.Error will also report the benign bug. It looks like changing the SourceLevels will not work

  • Came across this while having the same problem.

    These binding errors seem only to happen when using the Visual Studio Hosting Process (well in my case anyway). I found that if I debugged with that unchecked (in Project Properties > Debug), I didn't get those errors.

    Anyway, I know this post is old but thought it might help someone.

  • I found your post very helpful. I decided to not change the error level like your solution 2 but use a style change instead. While it may seem impractical to add these changes to each individual item, if you have a style set globally (like in App.xaml) and you are making the TargetType ListBoxItem for example, adding <Setter Property="HorizontalContentAlignment" Value="Left" /> and <Setter Property="VerticalContentAlignment" Value="Center" /> is no real big deal, as its a 2 line fix for every ListBoxItem, and you don't have to change the error log levels.

    Thank you for this help though, certainly cleared up my console so I am able to read the useful errors and outputs of my application :D

  • Using FallbackValue changed this error into a warning. Less annoyind but still too annoying,

    I faces this error when i bind a property to ScaleTransform's ScaleX and ScaleY properties. I tried your Solution1 but couldn't manage to make it work.Since ScaleTransform has no Style.

Add a Comment

As it will appear on the website

Not displayed

Your website