Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

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

4.88/5 (6 votes)
13 Nov 2010CPOL 39.4K  
Suppress the System.Windows.Data Error warning message in Library
When we use FindAncesstor in custom control template for binding internal elements property into its ancestor element, Visual Studio displays data warning messages in output window when binding engine meets unmatched target type during visual tree traversal though it does the proper binding when it receives expected target type during visual tree traversal. To suppress these data error warning messages, you should mention level of warning message trace in your library class.

Here is the simple data error that I am getting when I bind some commands in rich text box.

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


The solution is, set data binding source switch level as critical in constructor of the class.

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


Enjoy !!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)