views:

70

answers:

2

I'm dealing with a large WPF application that is outputting a large number of binding errors. A typical error looks like this:

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 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

Problem is I don't know where in the app this is coming from. Searching the entire solution for AncestorType={x:Type ItemsControl} doesn't necessary help since I still don't know which result is the culprit. I've tried setting PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.All; but the extra information doesn't help locate the problematic bindings. File names and line numbers is really what I need. Is there anyway to get this information? The only other solution I can think of is to assign names to the canidates then narrow it down from there. In that vein, is there a way to automatically assign a unique x:Name to all namable elements throughout the solution?

A: 

When do you see these binding errors? Only during startup (or loading of a new AddIn, or new View, etc.).

In those cases, I've found that these errors are often "false positives," in that they are thrown before the object being bound to is initialized. It's annoying, for sure, but if everything in your application seems to be bound correctly (here, for instance, your menus seem to have a proper HorizontalContentAlignment), then I wouldn't worry too much.

Wonko the Sane
I'm seeing a lot of binding errors. Although some may be "false positives", most aren't (judging from fixes I've already made). So, I'm still seeking an answer to my original question.
A: 

I generally set a breakpoint on the code that actually writes the error message. When the breakpoint is hit I inspect the visual tree of the object that had the binding error using a Visual Studio visualizer. This almost always tells me exactly where in my code to find the binding and fix it. (You can figure out where to set the breakpoint by installing a custom trace listener and breaking in it, then checking out the call stack.)

I also design my application to minimize the number of "false positives" by making sure bindings will always be valid when the source is present at all.

Ray Burns
Thanks for the suggestion, but I already tried it and the stack trace is nearly empty: > BindingListener.WriteLine(string message) Line 78 C# > [External Code]
Aha! I see your problem. Most of your stack trace is hidden because your Visual Studio installation is set to hide external code in the stack trace. This will really handicap your debugging because you can't see full stack traces or explore parameters and local variables outside your own code. I believe the procedure to fix this is to turn off "Enable Just My Code" in the Debugging tab under Tools > Options, then right-click the stack trace and select "Show External Code".
Ray Burns