Find binding errors

I am dealing with a large WPF application that is throwing a lot 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')

      

The problem is, I don't know where in the application this is happening. Searching for the whole solution for AncestorType = {x: Type ItemsControl} is unnecessary as I still don't know which result is the culprit. I tried installing PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.All;

but the additional information does not help to find the problematic bindings. The file names and line numbers really need me. Is there anyway to get this information? The only other solution I can think of is to assign names to the canadejs and then narrow them down from there. In this vein, is there a way to automatically assign a unique x: Name to all the elements that can be found in the solution?

+2


source to share


3 answers


I usually set a breakpoint on the code that actually writes the error message. When the breakpoint is hit, I check the visual tree of the object that had the binding error with the Visual Studio renderer. 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 setting a custom trace listener and hacking it, and then checking the call stack.)



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

+3


source


When do you see these binding errors? Only at startup time (or loading a new AddIn, or a new view, etc.).



In these cases, I have found that these errors are often "false positives" as they are thrown before the object associated with it is initialized. This is annoying, of course, but if everything in your application seems to be connected correctly (here, for example, your menus seem to have the correct HorizontalContentAlignment), then I wouldn't bother too much.

+1


source


Sounds like this: Xceed column selection error: http://xceed.com/CS/forums/thread/25140.aspx http://xceed.com/CS/forums/ShowThread.aspx?PostID=26315&PermaPostID=26029

I have the same problem, although the workarounds mentioned in this post don't work for me. Perhaps they will work for you.

0


source







All Articles