// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.Collections.ObjectModel;
namespace System.Windows.Controls.DataVisualization
{
///
/// An observable collection that can only be written to by internal
/// classes.
///
/// The type of object in the observable collection.
///
internal class ReadOnlyObservableCollection : NoResetObservableCollection
{
///
/// Gets or sets a value indicating whether the owner is writing to the
/// collection.
///
private bool IsMutating { get; set; }
///
/// A method that mutates the collection.
///
/// The action to mutate the collection.
public void Mutate(Action> action)
{
IsMutating = true;
try
{
action(this);
}
finally
{
IsMutating = false;
}
}
///
/// Removes an item from the collection at an index.
///
/// The index to remove.
protected override void RemoveItem(int index)
{
if (!IsMutating)
{
throw new NotSupportedException(Properties.Resources.ReadOnlyObservableCollection_CollectionIsReadOnly);
}
else
{
base.RemoveItem(index);
}
}
///
/// Sets an item at a particular location in the collection.
///
/// The location to set an item.
/// The item to set.
protected override void SetItem(int index, T item)
{
if (!IsMutating)
{
throw new NotSupportedException(Properties.Resources.ReadOnlyObservableCollection_CollectionIsReadOnly);
}
else
{
base.SetItem(index, item);
}
}
///
/// Inserts an item in the collection.
///
/// The index at which to insert the item.
/// The item to insert.
protected override void InsertItem(int index, T item)
{
if (!IsMutating)
{
throw new NotSupportedException(Properties.Resources.ReadOnlyObservableCollection_CollectionIsReadOnly);
}
else
{
base.InsertItem(index, item);
}
}
///
/// Clears the items from the collection.
///
protected override void ClearItems()
{
if (!IsMutating)
{
throw new NotSupportedException(Properties.Resources.ReadOnlyObservableCollection_CollectionIsReadOnly);
}
else
{
base.ClearItems();
}
}
}
}