Based on @Matt Hamilton's accepted answer, for reference, I created a variation which does not require the binding, as it uses the HasItems
property of the ListBox
to trigger the empty template:
<ListBox.Style>
<Style x:Key="EmptyListStyle"
TargetType="ListBox"
BasedOn="{StaticResource {x:Type ListBox}}">
<Style.Triggers>
<!-- Use ListBox.HasItems instead of Binding -->
<Trigger Property="HasItems" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock>No items to display</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
This allows the style to be used globally, without having to know the property name to which the list binds. I found it useful when binding to a CollectionViewSource
defined in XAML.
I'm not aware of any drawbacks of this method, comments welcome if you should find any.