WPF列表视图空的文本视图、文本、列表、WPF

2023-09-03 05:25:22 作者:Phantom(幻想)

使用GridView的一个空的文本(如在ASP.net)在WPF列表视图如何显示,如: 请人或0项目创办?

How to show in the WPF Listview using the GridView an empty text (like in ASP.net), e.g. "please select a person" or "0 items founded"?

推荐答案

此XAML会做同样的事情,它有一个明显的ListView显示列表和一个隐藏的信息和交换机的知名度,当列表为空使用触发器。

This XAML will do something similar, it has a visible ListView showing a list and a hidden message and switches visibility when the list is empty using a trigger.

在code以下将与所有的IList或ICollection的,但同样的技术工作,可与任何数据源的使用,像往常一样,如果你想更新显示,当您添加或删除你需要使用一个ObservableCollection项目或相似的。

The code below will work with any IList or ICollection but the same technique can be used with any data source, like always, if you want the display to update when you add or remove items you need to use an ObservableCollection or similar.

的内容presenter是存在的,因为你只能使用触发器模板或样式里面,所以我们把我们的控制里面的DataTemplate和使用Content presenter表现出来。

The ContentPresenter is there because you can only use triggers inside a template or a style, so we put our controls inside a DataTemplate and use the ContentPresenter to show it.

如果你想要显示的ListView不是所有你需要做的就是删除隐藏的ListView的二传手,并添加一定的余量到TextBlock来定位它的地方在ListView中的第一项应是内部的消息。

If you want the message to appear inside the ListView than all you have to do is remove the Setter that hides the ListView and add some margin to the TextBlock to position it where the first item in the ListVIew should be.

<ContentPresenter Content="{Binding}">
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <Grid>
                <ListView Name="list" ItemsSource="{Binding MyList}"/>
                <TextBlock Name="empty" Text="No items found" Visibility="Collapsed"/>
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding MyList.Count}" Value="0">
                    <Setter TargetName="list" Property="Visibility" Value="Collapsed"/>
                    <Setter TargetName="empty" Property="Visibility" Value="Visible"/>
                </DataTrigger>                        
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>