WPF ListBox - Display Message When Item Source Is Empty

bryon 101 Reputation points
2023-01-25T01:47:40.06+00:00

Hello All -

I am trying to replace the listbox with a message for the user when there is no data available. When there is data available, whatever is available, will display out. I have been working on this for a while but do not know what to do.

When the collection is empty, only the title displays, which is not what I want. I expected the message to tell the user there is no data.

I can populate the rows with data, so the bindings work fine. Any thoughts or ideas?


    public class MyData
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; }
    }


    <UserControl.Resources>
        <Style TargetType="ListView" x:Key="ListViewStyle">
            <Style.Triggers>
                <Trigger Property="HasItems" Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListView">
                                <TextBlock Text="Nothing to display" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>


  <ListView Style="{DynamicResource ListViewStyle}" ItemsSource="{Binding MyDataX}">
                <ListBox.Template>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <Grid DockPanel.Dock="Top" Height="30">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Column="0" HorizontalAlignment="Center">Id Number</Label>
                                <Label Grid.Column="1" HorizontalAlignment="Center">Desctiption</Label>
                                <Label Grid.Column="2" HorizontalAlignment="Center">Date</Label>
                            </Grid>
                            <ItemsPresenter></ItemsPresenter>
                        </DockPanel>
                    </ControlTemplate>
                </ListBox.Template>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Border BorderThickness="1" BorderBrush="Gray">
                            </Border>
                            <Grid Margin="0,2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Property1}" HorizontalAlignment="Center"></TextBlock>
                                <TextBlock Text="{Binding Property1}" Grid.Column="1" HorizontalAlignment="Center"></TextBlock>
                                <TextBlock Text="{Binding Property1}" Grid.Column="2" HorizontalAlignment="Center"></TextBlock>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListView>

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
766 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-01-25T02:44:36.6+00:00

    There are two good answers in this post: WPF listbox empty datatemplate

    One of them that I tried with a little modification:

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
            <Style TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}" Value="0">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBlock>No items to display</TextBlock>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <Grid>
             
        </Grid>
    </Window>
    

  2. Hui Liu-MSFT 40,266 Reputation points Microsoft Vendor
    2023-01-25T06:10:16.9533333+00:00

    As Reza Aghaei said, you could use Empty Template style. The code is show as follows.

     <Window.Resources>
            <Style TargetType="ListBox" x:Key="ListStyle" BasedOn="{StaticResource {x:Type ListBox}}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <DockPanel LastChildFill="True">
                                <Grid DockPanel.Dock="Top" Height="30">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <Label Grid.Column="0" HorizontalAlignment="Center">Id Number</Label>
                                    <Label Grid.Column="1" HorizontalAlignment="Center">Desctiption</Label>
                                    <Label Grid.Column="2" HorizontalAlignment="Center">Date</Label>
                                </Grid>
                                <ItemsPresenter></ItemsPresenter>
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger    Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}"  Value="0"  >
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBlock>No items to display</TextBlock>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <Grid>
            <ListBox  Style="{DynamicResource ListStyle}" ItemsSource="{Binding MyDataX}">
                
            
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Border BorderThickness="1" BorderBrush="Gray">
                            </Border>
                            <Grid Margin="0,2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Property1}" HorizontalAlignment="Center"></TextBlock>
                                <TextBlock Text="{Binding Property1}" Grid.Column="1" HorizontalAlignment="Center"></TextBlock>
                                <TextBlock Text="{Binding Property1}" Grid.Column="2" HorizontalAlignment="Center"></TextBlock>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    

    The result: enter image description here


    If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments