Display text if there are no items in the list of linked lists

The following is the syntax of my list bound to a class ....

<ListView ItemContainerStyle="{StaticResource listViewStyle}" Name="transactionListView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}" MouseDoubleClick="transactionListView_MouseDoubleClick" IsSynchronizedWithCurrentItem="True" >
    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{StaticResource gridViewHeaderColumnStyle}">
            <GridView.Columns>
                <GridViewColumn Width="70" Header="Serial" DisplayMemberBinding="{Binding Path=Serial}" />
                <GridViewColumn Width="100" Header="Date" DisplayMemberBinding="{Binding Path=Date, StringFormat={}{0:dd-MM-yyyy}}" />
                <GridViewColumn Width="200" Header="Seller" DisplayMemberBinding="{Binding Path=Seller}" />
                <GridViewColumn Width="200" Header="Buyer" DisplayMemberBinding="{Binding Path=Buyer}" />
                <GridViewColumn Width="70" Header="Bales" DisplayMemberBinding="{Binding Path=Bales}" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

* How can I display text when the list is empty or contains no items?

+5
source share
2 answers

The trick is to override the ListView template. When there are no items in the ListView, you must set the ControlTemplate using a TextBlock:

<ListView Name="List" ItemsSource="{Binding Items}">
    <ListView.Style>
        <Style TargetType="ListView">
            <Style.Triggers>
                <Trigger Property="HasItems"
                         Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListView">
                                <TextBlock Text="No items..."/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.Style>
</ListView>
+16
source

ListView itself does not provide this feature. The easiest approach is to place a TextBlock in front of the ListView with the Visibility value set for Collapsed. You can then make it visible if there are no items in the list.

, , .

+2

All Articles