What’s the best way to update an ObservableCollection from another thread?

If you initialize the collection in the constructor it will be on the default Application thread. To invoke the main thread you can do this: Application.Current.Dispatcher.Invoke((Action)(() => { //Do something here. })); You have to cast the Anonymous delegate as an action otherwise it gets confused ¯\O_o/¯ If you are using the Async CTP then … Read more

ObservableCollection and threading [duplicate]

JaredPar’s approach is a valid one. Another approach which is worth considering is using a thread safe ObservableCollection instead of the built-in ObservableCollection. There’s a few implementations out there, but Sasha Barber’s implementation and the CLinq Continuous Collection class are some of the better ones in my opinion. Internally, these classes essentially use the approach … Read more

WPF Datagrid Row Editing “ENDED” event

private void dgrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { if (this.dgrid.SelectedItem != null) { (sender as DataGrid).RowEditEnding -=dgrid_RowEditEnding; (sender as DataGrid).CommitEdit(); (sender as DataGrid).Items.Refresh(); (sender as DataGrid).RowEditEnding += dgrid_RowEditEnding; } else Return; //then check if the newly added row is duplicated }

A better way of forcing data bound WPF ListBox to update?

I have a Listbox bound to an object property which is of type List<MyCustomType>() and I verified that the following code updates the listbox when the List is updated. void On_MyObjProperty_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { MyListBox.Items.Refresh(); } If you’re still facing issues, scan the VS IDE output window (Ctrl+W, O) and see if you can … Read more

Adding a range of values to an ObservableCollection efficiently

The ObservableCollection exposes an protected Items property which is the underlying collection without the notification semantics. This means you can build a collection that does what you want by inheriting ObservableCollection: class RangeEnabledObservableCollection<T> : ObservableCollection<T> { public void InsertRange(IEnumerable<T> items) { this.CheckReentrancy(); foreach(var item in items) this.Items.Add(item); this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } Usage: void Main() { … Read more

WPF MVVM INotifyPropertyChanged Implementation – Model or ViewModel

The thing is that if you were following MVVM, you would have a BookViewModel for your Book model class. So you would have a INotifyPropertyChanged implementation on that view model. Exactly for that purpose MVVM exists (but not only). That being said, the INotifyPropertyChanged has to be implemented on view model classes, not models. UPDATE: … Read more

.NET ObservableDictionary

The Microsoft ParallelExtensionsExtras provides this class which is not only observable but is also concurrent: Now available via Nuget: https://www.nuget.org/packages/MSFT.ParallelExtensionsExtras/ The source code was recently updated for .NET Standard 2.1 on 05/11/2020 and the source code is available at GitHub: https://github.com/dotnet/samples/tree/master/csharp/parallel/ParallelExtensionsExtras Note that some of the features are now a part of newer .NET frameworks. … Read more

How can I make a read-only ObservableCollection property?

A solution is: public class Source { Source() { m_collection = new ObservableCollection<int>(); m_collectionReadOnly = new ReadOnlyObservableCollection<int>(m_collection); } public ReadOnlyObservableCollection<int> Items { get { return m_collectionReadOnly; } } readonly ObservableCollection<int> m_collection; readonly ReadOnlyObservableCollection<int> m_collectionReadOnly; } See ReadOnlyObservableCollection anti-pattern for a full discussion. Note that the [previously] accepted answer will actually return a different ReadOnlyObservableCollection every … Read more

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