WPF: How do I debug binding errors?

前端 未结 2 1621
别跟我提以往
别跟我提以往 2021-02-05 19:20

I\'m getting this in my output Window:

System.Windows.Data Error: 4 : Cannot find source for binding with reference \'RelativeSource FindAncestor, Ancesto

2条回答
  •  时光说笑
    2021-02-05 19:51

    I was also going to recommend Bea Stollnitz's article but Jonathan Allen got his post in while I was still typing this one. I also recommend the links in this blog entry.

    In this particular case you can see that somewhere a ListBoxItem has a FindAncestor binding to an ItemsControl that is failing. That tells you right away there is a ListBoxItem somewhere that is either:

    1. Not in the visual tree, or
    2. Not under an ItemsControl (a ListBox is an ItemsControl)

    In addition, you know that someone, somewhere, is binding a ListBoxItem's VerticalContentAlignment property to FindAncestor.

    Looking at the system themes (shipped with Expression Blend and also available through NET Reflector's BAMLViewer Add-in), we see this: