ListBoxItem generates the error "System.Windows.Data Error: 4" error

I created a ListBox file:

 <ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged"> <ListBox.Resources> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <Style.Triggers> <!--This trigger is needed, because RelativeSource binding can only succeeds if the current ListBoxItem is already connected to its visual parent--> <Trigger Property="IsVisible" Value="True"> <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> </Trigger> </Style.Triggers> </Style> </ListBox.Resources> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Margin="0,2,0,0"> <TextBlock Text="{Binding Number}" /> <StackPanel Orientation="Vertical" Margin="7,0,0,0"> <TextBlock Text="{Binding File}" /> <TextBlock Text="{Binding Dir}" Foreground="DarkGray" /> </StackPanel> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

This will cause a line to be sent to the OutputWindow from VisualStudio at runtime:

 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 'ListBoxItem' (Name=''); 

Can someone give me a hint how can I solve this?

Update

I added properties to the style to try to eliminate the warning / error.

+23
source share
3 answers

The easiest way to resolve this issue is to make sure the Listbox has an ItemContainerStyle . See the following example:

 <ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="VerticalContentAlignment" Value="Center"/> </Style> </ListBox.ItemContainerStyle> ... </ListBox> 

It happens that your objects are created, and by default they look for a parent property that is not defined. Explicit definition of a problem will solve this problem.

I had the same issue using TreeView and changing the linked source for these templates would trigger these warnings.

+33
source

The answer to this question solved this problem for me:

ListBox with a grid like ItemsPanelTemplate creates weird binding errors

Defining a top-level style (in my App.xaml) aimed at a fixed type problem for me. Here is a style that should work for you:

 <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="HorizontalContentAlignment" Value="Left" /> <Setter Property="VerticalContentAlignment" Value="Top" /> </Style> 

In my case, I created some TreeViewItems and then linked my TreeView to the created elements. A binding error occurred because the TreeViewItem binding was resolved before they were added to the TreeView. The correct solution was to not create a TreeViewItem, but instead create a class containing the data I need (Header and Items). Just convey my situation if there are parallels with your own.

+19
source

Another workaround that worked for me was to suppress these errors (in fact, it is more appropriate to call them warnings) by setting the switching level of the data binding source as critical in the constructor of the class or top-level window -

 #if DEBUG System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical; #endif 

Ref .: How to suppress the System.Windows.Data Error error message

Update: this is not the best solution, but for malware it looks good to me.

+1
source

Source: https://habr.com/ru/post/981776/


All Articles