WPF:我如何调试绑定错误? [英] WPF: How do I debug binding errors?

查看:222
本文介绍了WPF:我如何调试绑定错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的输出窗口得到这个:


  

System.Windows.Data错误:4:无法为参照的RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1'的结合找到源头。 BindingEx pression:路径= VerticalContentAlignment;的DataItem = NULL;目标元素是'ListBoxItem的'(名称='');目标属性是'VerticalContentAlignment(输入VerticalAlignment')


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

 <分组框标题=准许/拒绝报告>
            < ListBox中的ItemsSource ={结合转换器= {StaticResource的MethodBinder},ConverterParameter = GrantDeny,模式=单向}>
                < ListBox.ItemTemplate>
                    <&DataTemplate的GT;
                        < StackPanel的方向=横向>
                            <标签内容={结合实体}/>
                            <标签内容={结合HasPermission}/>
                        < / StackPanel的>
                    < / DataTemplate中>
                < /ListBox.ItemTemplate>
            < /列表框>
        < /分组框中>


解决方案

我也打算建议比亚Stollnitz的文章,但乔纳森 - 阿伦得到了他的职务,而在我还在打字这一个。我还建议在<一个链接href=\"http://wpfdisciples.word$p$pss.com/2008/04/04/debugging-in-wpf-wpf-disciples-tips-and-tricks/\">this博客条目。

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


  1. 不要在可视化树,或

  2. 不在一个ItemsControl(列表框是一个ItemsControl)

另外,你知道有人在什么地方,是一个ListBoxItem中的VerticalContentAlignment属性绑定到FindAncestor。

纵观系统主题(随前pression Blend和也可通过NET的反射的BAMLViewer加载项),我们可以看到这一点:

 &LT;风格X:键={X:输入一个ListBoxItem}&GT;
  &LT; setter属性=VerticalContentAlignment
          VALUE ={绑定路径= VerticalContentAlignment,的RelativeSource = {的RelativeSource AncestorType = {X:类型的ItemsControl}}/&GT;

这说明那里的约束力从何而来。是,是如何被创建的下一个问题一个ListBoxItem中不是一个列表框(或其他的ItemsControl)下?

有些事情要查找:


  • 您在构建code ListBoxItems地方?

  • 有没有在你的XAML明确指定的?任何一个ListBoxItem

  • 请您有任何code是手工操作在ListBox中的项目?

希望这将头你在正确的方向。

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')

This is my XAML, which when run looks correct

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

解决方案

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:

<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)?

Some things to look for:

  • Are you constructing ListBoxItems in code anywhere?
  • Is there any ListBoxItem explicitly specified in your XAML?
  • Do you have any code that manually manipulates the items in ListBox?

Hopefully this will head you in the right direction.

这篇关于WPF:我如何调试绑定错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆