WPF:如何调试绑定错误?

时间:2022-10-22 07:22:48

I'm getting this in my output Window:

我在输出窗口中得到了这个:

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

System.Windows.Data错误:4:无法找到与引用'RelativeSource FindAncestor绑定的源,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1''。 BindingExpression:路径= VerticalContentAlignment;的DataItem = NULL; target元素是'ListBoxItem'(Name =''); target属性是'VerticalContentAlignment'(类型'VerticalAlignment')

This is my XAML, which when run looks correct

这是我的XAML,运行时看起来是正确的

        <GroupBox Header="Grant/Deny Report">
            <ListBox ItemsSource="{Binding  Converter={StaticResource MethodBinder}, ConverterParameter=GrantDeny, Mode=OneWay}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="{Binding Entity}"/>
                            <Label Content="{Binding HasPermission}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </GroupBox>

2 个解决方案

#1


9  

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.

我还打算推荐Bea Stollnitz的文章,但Jonathan Allen在我还在输入这篇文章的时候得到了他的职位。我还推荐此博客条目中的链接。

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:

在这种特殊情况下,您可以看到ListBoxItem的某个地方有FindAncestor绑定到失败的ItemsControl。这告诉你马上有一个ListBoxItem,它是:

  1. Not in the visual tree, or
  2. 不在视觉树中,或
  3. Not under an ItemsControl (a ListBox is an ItemsControl)
  4. 不在ItemsControl下(ListBox是ItemsControl)

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

此外,您知道有人在某处将ListBoxItem的VerticalContentAlignment属性绑定到FindAncestor。

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

查看系统主题(Expression Blend附带,也可以通过NET Reflector的BAMLViewer外接程序获得),我们看到:

<Style x:Key="{x:Type ListBoxItem}">
  <Setter Property="VerticalContentAlignment"
          Value="{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}" />

This explains where the binding comes from. The next question is, how is a ListBoxItem being created that is not under a ListBox (or other ItemsControl)?

这解释了绑定的来源。接下来的问题是,如何创建一个不在ListBox(或其他ItemsControl)下的ListBoxItem?

Some things to look for:

有些事要寻找:

  • Are you constructing ListBoxItems in code anywhere?
  • 你在任何地方的代码中构建ListBoxItems?
  • Is there any ListBoxItem explicitly specified in your XAML?
  • 是否在您的XAML中明确指定了任何ListBoxItem?
  • Do you have any code that manually manipulates the items in ListBox?
  • 你有任何手动操作ListBox中的项目的代码吗?

Hopefully this will head you in the right direction.

希望这会引导您朝着正确的方向前进。

#2


3  

I ran into a similar problem with the TreeView (though my data binding errors showed up as being Informational).

我遇到了与TreeView类似的问题(虽然我的数据绑定错误显示为信息)。

I solved the problem by defining an implicit style in the TreeView resource for TreeViewItem. Within that style I defined the missing vertical and horizontal content alignment properties.

我通过在TreeViewItem的TreeView资源中定义隐式样式来解决问题。在该样式中,我定义了缺少的垂直和水平内容对齐属性。

<TreeView.Resources>
     <Style TargetType="{x:Type TreeViewItem}" >
          <Setter Property="VerticalContentAlignment" Value="Stretch"/>
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
     </Style>
</TreeView.Resources>

#1


9  

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.

我还打算推荐Bea Stollnitz的文章,但Jonathan Allen在我还在输入这篇文章的时候得到了他的职位。我还推荐此博客条目中的链接。

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:

在这种特殊情况下,您可以看到ListBoxItem的某个地方有FindAncestor绑定到失败的ItemsControl。这告诉你马上有一个ListBoxItem,它是:

  1. Not in the visual tree, or
  2. 不在视觉树中,或
  3. Not under an ItemsControl (a ListBox is an ItemsControl)
  4. 不在ItemsControl下(ListBox是ItemsControl)

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

此外,您知道有人在某处将ListBoxItem的VerticalContentAlignment属性绑定到FindAncestor。

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

查看系统主题(Expression Blend附带,也可以通过NET Reflector的BAMLViewer外接程序获得),我们看到:

<Style x:Key="{x:Type ListBoxItem}">
  <Setter Property="VerticalContentAlignment"
          Value="{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}" />

This explains where the binding comes from. The next question is, how is a ListBoxItem being created that is not under a ListBox (or other ItemsControl)?

这解释了绑定的来源。接下来的问题是,如何创建一个不在ListBox(或其他ItemsControl)下的ListBoxItem?

Some things to look for:

有些事要寻找:

  • Are you constructing ListBoxItems in code anywhere?
  • 你在任何地方的代码中构建ListBoxItems?
  • Is there any ListBoxItem explicitly specified in your XAML?
  • 是否在您的XAML中明确指定了任何ListBoxItem?
  • Do you have any code that manually manipulates the items in ListBox?
  • 你有任何手动操作ListBox中的项目的代码吗?

Hopefully this will head you in the right direction.

希望这会引导您朝着正确的方向前进。

#2


3  

I ran into a similar problem with the TreeView (though my data binding errors showed up as being Informational).

我遇到了与TreeView类似的问题(虽然我的数据绑定错误显示为信息)。

I solved the problem by defining an implicit style in the TreeView resource for TreeViewItem. Within that style I defined the missing vertical and horizontal content alignment properties.

我通过在TreeViewItem的TreeView资源中定义隐式样式来解决问题。在该样式中,我定义了缺少的垂直和水平内容对齐属性。

<TreeView.Resources>
     <Style TargetType="{x:Type TreeViewItem}" >
          <Setter Property="VerticalContentAlignment" Value="Stretch"/>
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
     </Style>
</TreeView.Resources>