Jerry Nixon @Work: Showing “No Records” when a WPF ListBox is Empty.

Jerry Nixon on Windows

Tuesday, July 19, 2011

Showing “No Records” when a WPF ListBox is Empty.

It's just a better user experience to tell the user that the list is empty (has no items) than to simply show it as empty.

Here's the easiest way (just re-template the whole control when Items.Count is zero). Remember, if you are using an ItemsControl then you have to use the ItemsSource.Count instead.

Take a look:

<ListBox>
    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Items.Count,
                    RelativeSource={RelativeSource Self}}"  Value="0">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Border BorderThickness="1" BorderBrush="Black"
                                    Padding="10" Margin="10">
                                    <TextBlock>No items to display</TextBlock>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

PS: You might consider putting this as a global style to apply to List controls.