Disable Adding Item to Collection

Posted by Wonko the Sane on Stack Overflow See other posts from Stack Overflow or by Wonko the Sane
Published on 2010-06-09T14:18:37Z Indexed on 2010/06/09 14:22 UTC
Read the original article Hit count: 143

Filed under:
|
|
|

Hi All,

I'm sure there's an "easy" answer to this, but for the moment it escapes me.

In an MVVM application, I have a property that is a ObservableCollection, used for displaying some set of elements on the view.

private readonly ObservableCollection<MyType> mMyCollection = 
    new ObservableCollection<MyType>();
public ObservableCollection<MyType> MyCollection
{
    get { return mMyCollection; }
}

I want to restrict consumers of this collection from simply using the property to add to the collection (i.e. I want to prevent this from the view):

   viewModel.MyCollection.Add(newThing);   // want to prevent this!

Instead, I want to force the use of a method to add items, because there may be another thread using that collection, and I don't want to modify the collection while that thread is processing it.

public void AddToMyCollection(MyType newItem)
{
    // Do some thread/task stuff here
}

Thanks, wTs

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf