0
votes

during application run-time, I encountered the following errors: 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')

I tried to add the following Style code lines, but unfortunately it did not help. Any ideas?

code:

<ListView Visibility="{Binding Path=IsListDisplayed, Converter={StaticResource BooleanToVisConverter}}" 
              ScrollViewer.VerticalScrollBarVisibility="Auto"                      
              ScrollViewer.HorizontalScrollBarVisibility="Disabled"                  
              Grid.Row="1" 
              ItemsSource="{Binding JobsList}"
              SelectedItem="{Binding SelectedJob}"                      
              x:Name="JobsLv">

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>                    
                <avt_controls:VirtualizingWrapPanel/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>                
            <DataTemplate>
                <Border BorderThickness="4" CornerRadius="10" Margin="10">
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding JobStatus}" Value="Ready">
                                    <Setter Property="BorderBrush" Value="Green"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding JobStatus}" Value="FromMIS">
                                    <Setter Property="BorderBrush" Value="Blue"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding JobStatus}" Value="InProgress">
                                    <Setter Property="BorderBrush" Value="Red"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding JobName}" Grid.Row="0" FontSize="13" Margin="5,0,0,0"/>
                        <TextBlock Text="{Binding LastUpdate, StringFormat={}{0:dd/MM/yyyy hh:mm}}" Grid.Row="1" FontSize="13" Margin="5,0,0,0"/>
                        <ListView Grid.Row="2" ItemsSource="{Binding Designs}" BorderBrush="Transparent" Background="Transparent" Margin="3">
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"></StackPanel>
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Image Width="100" Margin="1">
                                        <Image.Style>
                                            <Style TargetType="{x:Type Image}">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsDownloading}" Value="True">
                                                        <Setter Property="Source" Value="{Binding ThumbnailFilePath}"/>
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IsDownloading}" Value="False">
                                                        <Setter Property="Source" Value="{Binding ThumbnailFilePath}"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Image.Style>
                                    </Image>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </Grid>
                </Border>
            </DataTemplate>                   
        </ListView.ItemTemplate>            
    </ListView>
1
Can you provide the full ListView XAML?Mike Eason
please look into the code above now. Thanks, Shaishai
Your question mentioned that your binding was trying to bind to HorizontalContentAlignment, but I don't see any mention of that in your code. Could you please update the question if this has changed? Thanks.Mike Eason

1 Answers

0
votes

(Late answer i know, but it's one of the first links i found so might be helpful to someone.)

I had the same issue with a simple WrapPanel as my ItemsPanel, for a listview with a complicated ItemTemplate (no weird alignment binding anywhere, though). It would complain about binding errors for HorizontalContentAlignment and VerticalContentAlignment at the moment the page with the ListView is created, and it would work fine afterwards.

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <WrapPanel Orientation="Horizontal"/>
  </ItemsPanelTemplate>
</ListView.ItemsPanel>

I replaced the WrapPanel with https://www.nuget.org/packages/VirtualizingWrapPanel/ since i want to virtualize anyway. The error is now gone. Mind that the Orientation works the opposite way compared to the standard WrapPanel, for some reason.

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <wpftk:VirtualizingWrapPanel Orientation="Vertical"/>
  </ItemsPanelTemplate>
</ListView.ItemsPanel>