- You cannot select a null value in combobox.
- You need to use an empty element to display it in the control.
I have the same problem and am using this solution in my current project:
public class ObservableCollectionCopy<T> : ObservableCollection<T> { public ObservableCollectionCopy(T firstItem, ObservableCollection<T> baseItems) { this.FirstItem = firstItem; this.Add(firstItem); foreach (var item in baseItems) this.Add(item); baseItems.CollectionChanged += BaseCollectionChanged; } public T FirstItem { get; set; } private void BaseCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) foreach (var newItem in e.NewItems.Cast<T>().Reverse()) this.Insert(e.NewStartingIndex + 1, newItem); if (e.OldItems != null) foreach (var oldItem in e.OldItems.Cast<T>()) this.Remove(oldItem); } }
The new collection has one-way binding to the base collection:
this.SelectableGroups = new ObservableCollectionCopy<GroupModel>( new GroupModel{Id = -1, Title = "Any group"}, this.GroupsCollection);
Filtration:
if (this.selectedGroup != null && this.selectedGroup.Id != -1) this.MyCollectionView.Filter = v => v.SomeItem.GroupId == this.selectedGroup.Id; else this.MyCollectionView.Filter = null;
source share