Search code examples
wpfdata-binding

In WPF, is there a way to declare the type of an ancestor's DataContext?


In a user control I am using the RelativeSource to bind to the command of the DataContext of the hosting window. I have used the d:DataContext to specify the type of the DataContext in both the User Control and the hosting window.

In the Command Binding I could specify the type of the hosting window using AncerstorType, but I can't see any way to specify the type of its DataContext. It would be nice to have a design time warning in case the property of that DataContext is mistyped or do not exist, and to eliminate the squiggly blue underlines.


Solution

  • I would like to be warned at compile time if I refer to a non-existent property [See Comment]

    #if DEBUG
        System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level =
        System.Diagnostics.SourceLevels.All;
    #endif
    

    To open XAML Binding Failures go to Debug > Windows

    enter image description here