Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find source for binding with reference... databound ListView problem

I know that there have been questions regarding this error, I've found some and read them but to be honest, I didn't understand a thing.

I have a WPF window with two databound ListViews. One is bound to business objects (my custom classes), another to a Dictionary<string, string>. Everything seems to look OK in runtime, but I'm getting errors in the 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=HorizontalContentAlignment; DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

and the same for VerticalContentAlignment.

Even though bost ListViews get populated with items as expected, it actually causes a noticeable delay when loading the window.

Looking for an answer, I found this thread http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768 - and I implemented the suggested solution, supplying default values of both HorizontalContentAlignment and VerticalContentAlignment in both ListViews. It didn't help.

Here's the XAML:

  • ListView 1:

                    <ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="13" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                        <ListView.Resources>
                            <Style TargetType="ListViewItem">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                <Setter Property="VerticalContentAlignment" Value="Stretch" />
                            </Style>
                        </ListView.Resources>
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="3" />
                            </ItemsPanelTemplate>
                        </ListView>
    
  • ListView 2

                            <ListView.Resources>
                                <Style TargetType="ListViewItem">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                    <Setter Property="VerticalContentAlignment" Value="Stretch" />
                                    <EventSetter Event="Selected" Handler="lvItemSelected" />
                                </Style>
                                <Style x:Key="GrayOutMappedColumn" TargetType="{x:Type TextBlock}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Mapped}" Value="False">
                                            <Setter Property="TextElement.Foreground" Value="Black" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                    <Setter Property="TextElement.Foreground" Value="DarkGray" />
                                </Style>
                            </ListView.Resources>
                            <ListView.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <Border BorderBrush="LightGray" BorderThickness="0,0,0,1">
                                                <TextBlock FontSize="12" FontWeight="Bold" Margin="0,10" Text="{Binding Name}" />
                                            </Border>
                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ListView.GroupStyle>
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <UniformGrid Columns="4" />
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel ClipToBounds="False" HorizontalAlignment="Stretch" Width="Auto">
                                        <TextBlock HorizontalAlignment="Stretch" Style="{StaticResource GrayOutMappedColumn}" Text="{Binding Path=FriendlyName}" Width="Auto" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
    

Data-binding code:

lvLanguageCodes.ItemsSource = languages;
lvLanguageCodes.SelectedValuePath = "Key";
lvLanguageCodes.DisplayMemberPath = "Value";

2:

lvDataTypes.ItemsSource = AssignDataType.datatypes;

where datatypes is ObservableCollection<Gate>, where Gate is my business class (implementing INotifyPropertyChanged, IComparable and nothing special about it otherwise).

Why am I getting an error? Why is it trying to bind these alignment properties to anything at all, if I set their values explicitly?

like image 751
Konrad Morawski Avatar asked Aug 05 '11 16:08

Konrad Morawski


2 Answers

You need to override the default ItemContainerStyle. So Blam's answer is half-correct. Here is mine:

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
    ...
</Style>

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"
like image 80
Charlie Avatar answered Oct 30 '22 12:10

Charlie


Try below. Not sure it will work in your environment but this is the syntax that is working for me.

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

It may be a a data binding issue. Could you try?

    <ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="1" 
              PresentationTraceSources.TraceLevel="High" 
              DisplayMemberPath="Value" SelectedValuePath="Key" ItemsSource="Langages">
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.Resources>
    </ListView>

You do know ListView.ItemsPanel is not closed out. I am surprised this compiles.

like image 1
paparazzo Avatar answered Oct 30 '22 13:10

paparazzo