WPF Listview blank text

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

+5
source share
2 answers

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

The code below will work with any IList or ICollection, but the same method can be used with any data source, as always, if you want the display to be updated when adding or removing elements that need to be used by ObservableCollection or similar.

ContentPresenter exists because you can use triggers inside a template or style, so we put our controls in a DataTemplate and use ContentPresenter to display it.

If you want the message to appear inside the ListView, all you have to do is delete the Setter, which hides the ListView and adds some field to the TextBlock to place 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>
+10
source

DataSource +, , ? , .

-1

All Articles