ListBox with Grid as ItemsPanelTemplate produces weird binding errors ListBox with Grid as ItemsPanelTemplate produces weird binding errors wpf wpf

ListBox with Grid as ItemsPanelTemplate produces weird binding errors


The binding problem comes from the default style for ListBoxItem. By default when applying styles to elements WPF looks for the default styles and applies each property that is not specifically set in the custom style from the default style. Refer to this great blog post By Ian Griffiths for more details on this behavior.

Back to our problem. Here is the default style for ListBoxItem:

<Style    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:s="clr-namespace:System;assembly=mscorlib"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    TargetType="{x:Type ListBoxItem}">    <Style.Resources>       <ResourceDictionary/>    </Style.Resources>    <Setter Property="Panel.Background">       <Setter.Value>          <SolidColorBrush>        #00FFFFFF          </SolidColorBrush>       </Setter.Value>    </Setter>    <Setter Property="Control.HorizontalContentAlignment">       <Setter.Value>          <Binding Path="HorizontalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}"/>       </Setter.Value>    </Setter>    <Setter Property="Control.VerticalContentAlignment">       <Setter.Value>          <Binding Path="VerticalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}"/>       </Setter.Value>    </Setter>    <Setter Property="Control.Padding">       <Setter.Value>          <Thickness>        2,0,0,0          </Thickness>       </Setter.Value>    </Setter>    <Setter Property="Control.Template">       <Setter.Value>          <ControlTemplate TargetType="{x:Type ListBoxItem}">             ...          </ControlTemplate>       </Setter.Value>    </Setter> </Style>

Note that I have removed the ControlTemplate to make it compact (I have used StyleSnooper - to retrieve the style). You can see that there is a binding with a relative source set to ancestor with type ItemsControl. So in your case the ListBoxItems that are created when binding did not find their ItemsControl. Can you provide more info with what is the ItemsSource for your ListBox?

P.S.: One way to remove the errors is to create new setters for HorizontalContentAlignment and VerticalContentAlignment in your custom Style.


Setting OverridesDefaultStyle to True in your ItemContainerStyle will also fix these problems.

<Style TargetType="ListBoxItem">    <Setter Property="OverridesDefaultStyle" Value="True"/>    <!-- set the rest of your setters, including Template, here --></Style>


This is an amalgam of the other answers here, but for me, I had to apply the Setter in two places to solve the error, although this was when using a custom VirtualizingWrapPanel

If I remove either one of the below Setter declarations, my errors reappear.

        <ListView>            <ListView.Resources>                <Style TargetType="ListViewItem">                    <Setter Property="HorizontalContentAlignment" Value="Left" />                    <Setter Property="VerticalContentAlignment" Value="Top" />                </Style>            </ListView.Resources>            <ListView.ItemContainerStyle>                <Style TargetType="ListViewItem">                    <Setter Property="HorizontalContentAlignment" Value="Left" />                    <Setter Property="VerticalContentAlignment" Value="Top" />                </Style>            </ListView.ItemContainerStyle>            <ListView.ItemsPanel>                <ItemsPanelTemplate>                    <controls:VirtualizingWrapPanel />                </ItemsPanelTemplate>            </ListView.ItemsPanel>        </ListView>

I don't really have the time to investigate further at the moment, but I suspect it's related to the default style that JTango mentions in his answer - I'm not really customising my template to a huge degree.

I think there's more mileage to be had out of the other answers, but I thought I'd post this on the off chance it helps someone in the same boat.

David Schmitt's answer looks like it might describe the root cause.