views:

123

answers:

2

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>
+1  A: 

Here a good blog from Bea Stollnitz talking about DataBinding debugging

Zied
+1  A: 

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.

Ray Burns
To your questsions, No, No, and No. The only thing that's interesting is that the ItemsSource is a data table.
Jonathan Allen
But hey, at least you gave me enough information to know that it is in the style sheets.
Jonathan Allen