I have an ObservableCollection, and an ICollectionView using the OC as a source:
private ObservableCollection<Comment> _Comments = new ObservableCollection<Comment>();
/// <summary>
/// Comments on the account
/// </summary>
[BsonElement("comments")]
public ObservableCollection<Comment> Comments
{
get
{
return _Comments;
}
set
{
_Comments = value;
OnPropertyChanged("Comments");
OnPropertyChanged("CommentsSorted");
}
}
private ICollectionView _CommentsSorted;
/// <summary>
/// Sorted list (reverse order) of the comments
/// </summary>
[BsonIgnore]
public ICollectionView CommentsSorted
{
get
{
return _CommentsSorted;
}
set
{
_CommentsSorted = value;
OnPropertyChanged("CommentsSorted");
}
}
I have a command, which runs:
obj.Comments.Add(new Comment(Message));
where obj is an instance of the class containing the observable collection.
When calling this line, I am hitting the following exception:
System.NotSupportedException: 'This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.'
I have opened the Debug > Windows > Threads panel, and it is running on the main thread. I have tried putting it inside App.Current.Dispatcher.Invoke(...), no luck.
I can't figure out why this is happening. To make things stranger, I am able to run this fine, no problems at all, on another instance of the same class, which was created at the same time (returned and created together from my database in the same call). The first one I added a comment to no problem, and still can every time, yet all the others I have tried fail.