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 !!!