开发者

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 me开发者_开发技巧ssage in the listbox like "No Items Present"


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();
    }
}


use this method which is using Datatrigger of the listbox .

WPF listbox empty datatemplate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜