在ListView中显示文本而不是集合

时间:2011-02-14 14:20:54

标签: wpf listview

我使用ListViewGridView视图来显示搜索结果。在没有要显示的元素的情况下,我想在ListView的中间显示信息性消息(类似“找不到元素”)。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

当ListView没有项目时,您可以使用触发器更改ListView模板,如下所示:

    <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>