Often, when I'm showing lists, if they're empty, I'm showing alternative text.
I used to always have to make a converter to display this text.
And then I came up with the idea of inheriting from the Observable Collection to add HasItem property.
Now I'm directly bound to the HasItem property to see whether or not this alternative text is displayed.
What do you think?
public class HasItemObservableCollection<T> : ObservableCollection<T>
{
bool _hasItem;
protected override void ClearItems()
{
base.ClearItems();
FireHasItemChanged();
}
protected override void RemoveItem(int index)
{
base.RemoveItem(index);
FireHasItemChanged();
}
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
FireHasItemChanged();
}
void FireHasItemChanged()
{
bool oldHasItem = _hasItem;
_hasItem = Items.Any();
if (oldHasItem != HasItem)
base.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("HasItem"));
}
/// <summary>
/// Obtient si la collection à des Items
/// </summary>
public bool HasItem
{
get
{
return _hasItem;
}
}
}