Show text in ListView instead of collection
I use a ListView
with a GridView
view to display results of a search. I'd like to display an informative message (something like “No element开发者_StackOverflow社区s found”) in the middle of the ListView
in the case when there are no elements to display. How can I do this?
You could change ListView template with trigger in style when ListView has no items, like this:
<ListView Name="List"
DockPanel.Dock="Top"
ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="70" Header="Serial" DisplayMemberBinding="{Binding Path=Serial}" />
</GridView.Columns>
</GridView>
</ListView.View>
<ListView.Style>
<Style TargetType="ListView">
<Style.Triggers>
<Trigger Property="HasItems"
Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListView">
<StackPanel>
<ListView>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="70" Header="Serial" DisplayMemberBinding="{Binding Path=Serial}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<TextBlock Text="No items..."/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
精彩评论