Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Listview empty text

How to show in the WPF Listview using the GridView an empty text (like in ASP.net), e.g. "please select a person" or "0 items founded"?

like image 673
Alexander Zwitbaum Avatar asked Jun 19 '09 17:06

Alexander Zwitbaum


1 Answers

This XAML will do something similar, it has a visible ListView showing a list and a hidden message and switches visibility when the list is empty using a trigger.

The code below will work with any IList or ICollection but the same technique can be used with any data source, like always, if you want the display to update when you add or remove items you need to use an ObservableCollection or similar.

The ContentPresenter is there because you can only use triggers inside a template or a style, so we put our controls inside a DataTemplate and use the ContentPresenter to show it.

If you want the message to appear inside the ListView than all you have to do is remove the Setter that hides the ListView and add some margin to the TextBlock to position it where the first item in the ListVIew should be.

<ContentPresenter Content="{Binding}">
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <Grid>
                <ListView Name="list" ItemsSource="{Binding MyList}"/>
                <TextBlock Name="empty" Text="No items found" Visibility="Collapsed"/>
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding MyList.Count}" Value="0">
                    <Setter TargetName="list" Property="Visibility" Value="Collapsed"/>
                    <Setter TargetName="empty" Property="Visibility" Value="Visible"/>
                </DataTrigger>                        
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>
like image 184
Nir Avatar answered Oct 19 '22 11:10

Nir