batrulz
batrulz

Reputation: 181

How to display a default value for WPF ListBox bound to an empty list

If the Bounded List is empty I want to display a default message in the listbox like "No Items Present"

Upvotes: 3

Views: 4184

Answers (2)

Kishore Kumar
Kishore Kumar

Reputation: 21873

use this method which is using Datatrigger of the listbox .

WPF listbox empty datatemplate

Upvotes: 1

Elad Katz
Elad Katz

Reputation: 7601

You should a textBlock on top of the listbox, and bind its visibility to the collection also, using a converter that would convert null to Visibility.Collapsed.

<Grid>
  <ListBox ItemsSource="{Binding TheItems}" />
  <TextBlock Text="No Items Found" 
    Visibility="{Binding TheItems, Converter={StaticResource TheConverter}}" />
</Grid>

and the converter:

public class NullToInvisibleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value==null ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 4

Related Questions