Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display some text when binded listview has no items

Following is the syntax for my listview which is binded 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 some text when the list is empty or contain no items?

like image 689
Pankaj Upadhyay Avatar asked Feb 07 '11 05:02

Pankaj Upadhyay


2 Answers

The trick is in overriding ListView's template. When there are no items in ListView you should set your ControlTemplate with 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>
like image 73
Alex Zhevzhik Avatar answered Nov 11 '22 02:11

Alex Zhevzhik


The ListView itself does not supply this functionality. The simplest approach is to place a TextBlock in front of the ListView with its Visibility set to Collapsed. You can then make it Visible when your list has no items in it.

If you need help with the specific, please expand your question.

like image 2
ColinE Avatar answered Nov 11 '22 02:11

ColinE