绑定列表视图没有项目时显示一些文本

时间:2011-02-07 05:45:10

标签: .net wpf xaml listview

以下是我的listview的语法,它绑定到类....

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

*当列表为空或不包含任何项目时,如何显示某些文本?

2 个答案:

答案 0 :(得分:21)

诀窍是覆盖ListView的模板。当ListView中没有项目时,您应该使用TextBlock设置ControlTemplate:

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

答案 1 :(得分:2)

ListView本身不提供此功能。最简单的方法是将TextBlock放在ListView前面,并将其Visibility设置为Collapsed。然后,当您的列表中没有任何项目时,您可以将其设为可见。

如果您需要特定方面的帮助,请扩展您的问题。