Check if there is an ObservableCollection, and if so, display an alternate xaml!

I have a ListView bound to an ObservableCollection . Next, I list all the elements in the ObservableCollection . Now, is there a good way to check if an ObservableCollection empty and displays an alternate xaml?

+6
data-binding wpf xaml observablecollection
source share
1 answer

You can use the HasItems dependency property in a ListView. With a trigger, when a property is false, you can change the ControlTemplate. Here is an example:

 <ListView ItemsSource="{Binding Items}"> <ListView.Style> <Style TargetType="{x:Type ListView}"> <Style.Triggers> <Trigger Property="HasItems" Value="False"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListView}"> <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <TextBlock Text="No items" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </ListView.Style> </ListView> 
+8
source share

All Articles