WPF Listview empty text

后端 未结 2 1505
梦毁少年i
梦毁少年i 2021-01-12 04:35

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\"?

相关标签:
2条回答
  • 2021-01-12 05:00

    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.

    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.

    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.

    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>
    
    0 讨论(0)
  • 2021-01-12 05:20

    Bind it to a DataSource + Property that returns the text you want ? Slot in a dummy object whose String representation is the text you want..

    0 讨论(0)
提交回复
热议问题