How to remove DataGrid’s blank row when binding to a ObservableCollection?

The same problem persist in WPF 4.0 version of DataGrid, and it is caused by the add-new row which it shows automatically for ObservableCollection ItemsSource. Setting IsReadOnly as True it’s too radical IMHO. I solved it by disabling CanUserAddRows property if you don’t need that behavior, but you still want cells to be modified: CanUserAddRows=”False”

Optimal LINQ query to get a random sub collection – Shuffle

Further to mquander’s answer and Dan Blanchard’s comment, here’s a LINQ-friendly extension method that performs a Fisher-Yates-Durstenfeld shuffle: // take n random items from yourCollection var randomItems = yourCollection.Shuffle().Take(n); // … public static class EnumerableExtensions { public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) { return source.Shuffle(new Random()); } public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng) … Read more

WPF Multiple CollectionView with different filters on same collection

This answer helped me with this exact problem. The static CollectionViewSource.GetDefaultView(coll) method will always return the same reference for a given collection, so basing multiple collection views on the same reference will be counterproductive. By instantiating the view as follows: ICollectionView filteredView = new CollectionViewSource { Source=messageList }.View; The view can now be filtered/sorted/grouped independently … Read more

Notify ObservableCollection when Item changes

The spot you have commented as // Code to trig on item change… will only trigger when the collection object gets changed, such as when it gets set to a new object, or set to null. With your current implementation of TrulyObservableCollection, to handle the property changed events of your collection, register something to the … Read more

How to Avoid Firing ObservableCollection.CollectionChanged Multiple Times When Replacing All Elements Or Adding a Collection of Elements

ColinE is right with all his informations. I only want to add my subclass of ObservableCollection that I use for this specific case. public class SmartCollection<T> : ObservableCollection<T> { public SmartCollection() : base() { } public SmartCollection(IEnumerable<T> collection) : base(collection) { } public SmartCollection(List<T> list) : base(list) { } public void AddRange(IEnumerable<T> range) { foreach … Read more

ObservableCollection and Item PropertyChanged

Here is how you would attach/detach to each item’s PropertyChanged event. ObservableCollection<INotifyPropertyChanged> items = new ObservableCollection<INotifyPropertyChanged>(); items.CollectionChanged += items_CollectionChanged; static void items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) { foreach (INotifyPropertyChanged item in e.OldItems) item.PropertyChanged -= item_PropertyChanged; } if (e.NewItems != null) { foreach (INotifyPropertyChanged item in e.NewItems) item.PropertyChanged += item_PropertyChanged; } } … Read more

RemoveAll for ObservableCollections?

I am not aware of a way to remove only the selected items. But creating an extension method is straight forward: public static class ExtensionMethods { public static int Remove<T>( this ObservableCollection<T> coll, Func<T, bool> condition) { var itemsToRemove = coll.Where(condition).ToList(); foreach (var itemToRemove in itemsToRemove) { coll.Remove(itemToRemove); } return itemsToRemove.Count; } } This removes … Read more

Sort ObservableCollection through C#

Introduction Basically, if there is a need to display a sorted collection, please consider using the CollectionViewSource class: assign (“bind”) its Source property to the source collection — an instance of the ObservableCollection<T> class. The idea is that CollectionViewSource class provides an instance of the CollectionView class. This is kind of “projection” of the original … Read more

How do I update an ObservableCollection via a worker thread?

New option for .NET 4.5 Starting from .NET 4.5 there is a built-in mechanism to automatically synchronize access to the collection and dispatch CollectionChanged events to the UI thread. To enable this feature you need to call BindingOperations.EnableCollectionSynchronization from within your UI thread. EnableCollectionSynchronization does two things: Remembers the thread from which it is called … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)