Search code examples
wpfdata-binding

Style being ignored when ICollectionView refreshed


I have a viewmodel which defines a List<string>. This is exposed using a public ICollectionView:

List<string> _items { get; } = new List<string>() { "Test item" };
public ICollectionView CollectionView { get; }
....
        CollectionView = CollectionViewSource.GetDefaultView(_items);
        CollectionView.Filter = FilterItems;

The CollectionView object also defines a FilterItems function to filter the list.

On the UI side I have a ListBox which defines a data template and binds to CollectionView and everything works as expected. I initially got these warnings for each list box item:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

But I eliminated them by adding a style to the window which applies to all ListBoxItems:

<Window.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
    </Style>
</Window.Resources>

However, whenever I call Refresh() on CollectionView, the style doesn't seem to apply anymore, as the two warnings above come back for each list item! In fact, there are a couple of additional errors as well:

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=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
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')

What's happening here? Why is the style ignored when the collection is refreshed?

Oddly enough I can fix this by moving the style out of Window.Resource and putting it into Application.Resource in App.xaml but I don't understand why this is.


Solution

  • These binding errors are harmless and handled internally so you can safely just ignore them, or suppress them. Please refer to the following link for more information.

    Resolving harmless binding errors in WPF: https://weblogs.asp.net/akjoshi/resolving-un-harmful-binding-errors-in-wpf

    You may also want to read this thread on the MSDN forums.