Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / Controls / selecteditemcollection.cs / 1 / selecteditemcollection.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) 2008 by Microsoft Corporation. All rights reserved.
//
//
//
// Description: SelectedItemCollection holds the list of selected items of a Selector.
//
//
// History:
// 10/01/2008 : atanask - Created
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;
namespace System.Windows.Controls
{
///
/// This class represent the collection of SelectedItems in Selector. It extends the ObservableCollection by providing methods for bulk selection.
///
internal class SelectedItemCollection : ObservableCollection
{
#region Contructors
///
/// Create a new SelectedItemCollection object which keeps a reference to the corresponding Selector
///
///
public SelectedItemCollection(Selector selector)
{
_selector = selector;
}
#endregion
#region Protected Methods
///
/// Clear all items from the selection. This method modifies the behavior of IList.Clear()
///
protected override void ClearItems()
{
if (_updatingSelectedItems)
{
foreach (object current in _selector._selectedItems)
{
_selector.SelectionChange.Unselect(current);
}
}
else
{
base.ClearItems();
}
}
///
/// Removes an item from the selection. This method modifies the behavior of IList.Remove() and IList.RemoveAt()
///
protected override void RemoveItem(int index)
{
if (_updatingSelectedItems)
{
_selector.SelectionChange.Unselect(this[index]);
}
else
{
base.RemoveItem(index);
}
}
///
/// Inserts an item in the selection
///
protected override void InsertItem(int index, object item)
{
if (_updatingSelectedItems)
{
// For defered selection we should allow only Add method
if (index == Count)
{
_selector.SelectionChange.Select(item, true /* assumeInItemsCollection */);
}
else
{
throw new InvalidOperationException(SR.Get(SRID.InsertInDeferSelectionActive));
}
}
else
{
base.InsertItem(index, item);
}
}
///
/// Sets an item on specified index
///
protected override void SetItem(int index, object item)
{
if (_updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.SetInDeferSelectionActive));
}
else
{
base.SetItem(index, item);
}
}
///
/// Movea an item from one position to another
///
/// index of the column which is being moved
/// index of the column to be move to
protected override void MoveItem(int oldIndex, int newIndex)
{
if (oldIndex != newIndex)
{
if (_updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.MoveInDeferSelectionActive));
}
else
{
base.MoveItem(oldIndex, newIndex);
}
}
}
#endregion
#region MiltiSelector methods
///
/// Begin tracking selection changes. SelectedItems.Add/Remove will queue up the changes but not commit them until EndUpdateSelecteditems is called.
///
internal void BeginUpdateSelectedItems()
{
if (_selector.SelectionChange.IsActive || _updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.DeferSelectionActive));
}
_updatingSelectedItems = true;
_selector.SelectionChange.Begin();
}
///
/// Commit selection changes.
///
internal void EndUpdateSelectedItems()
{
if (!_selector.SelectionChange.IsActive || !_updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.DeferSelectionNotActive));
}
_updatingSelectedItems = false;
_selector.SelectionChange.End();
}
///
/// Returns true after BeginUpdateSelectedItems is called
///
internal bool IsUpdatingSelectedItems
{
get
{
return _selector.SelectionChange.IsActive || _updatingSelectedItems;
}
}
#endregion
#region Private data
// Keep a reference for Selector owner
private Selector _selector;
// We need a flag for indicating user bulk selection mode. We cannot re-use SelectionChange.IsActive because there are cases when SelectionChange.IsActive==true and SelectedItems.Add is called internally (End()) to update the collection
// When EndUpdateSelectedItems() is called we first reset this flag to allow SelectedItems.Add to change the collection
private bool _updatingSelectedItems;
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) 2008 by Microsoft Corporation. All rights reserved.
//
//
//
// Description: SelectedItemCollection holds the list of selected items of a Selector.
//
//
// History:
// 10/01/2008 : atanask - Created
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;
namespace System.Windows.Controls
{
///
/// This class represent the collection of SelectedItems in Selector. It extends the ObservableCollection by providing methods for bulk selection.
///
internal class SelectedItemCollection : ObservableCollection
{
#region Contructors
///
/// Create a new SelectedItemCollection object which keeps a reference to the corresponding Selector
///
///
public SelectedItemCollection(Selector selector)
{
_selector = selector;
}
#endregion
#region Protected Methods
///
/// Clear all items from the selection. This method modifies the behavior of IList.Clear()
///
protected override void ClearItems()
{
if (_updatingSelectedItems)
{
foreach (object current in _selector._selectedItems)
{
_selector.SelectionChange.Unselect(current);
}
}
else
{
base.ClearItems();
}
}
///
/// Removes an item from the selection. This method modifies the behavior of IList.Remove() and IList.RemoveAt()
///
protected override void RemoveItem(int index)
{
if (_updatingSelectedItems)
{
_selector.SelectionChange.Unselect(this[index]);
}
else
{
base.RemoveItem(index);
}
}
///
/// Inserts an item in the selection
///
protected override void InsertItem(int index, object item)
{
if (_updatingSelectedItems)
{
// For defered selection we should allow only Add method
if (index == Count)
{
_selector.SelectionChange.Select(item, true /* assumeInItemsCollection */);
}
else
{
throw new InvalidOperationException(SR.Get(SRID.InsertInDeferSelectionActive));
}
}
else
{
base.InsertItem(index, item);
}
}
///
/// Sets an item on specified index
///
protected override void SetItem(int index, object item)
{
if (_updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.SetInDeferSelectionActive));
}
else
{
base.SetItem(index, item);
}
}
///
/// Movea an item from one position to another
///
/// index of the column which is being moved
/// index of the column to be move to
protected override void MoveItem(int oldIndex, int newIndex)
{
if (oldIndex != newIndex)
{
if (_updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.MoveInDeferSelectionActive));
}
else
{
base.MoveItem(oldIndex, newIndex);
}
}
}
#endregion
#region MiltiSelector methods
///
/// Begin tracking selection changes. SelectedItems.Add/Remove will queue up the changes but not commit them until EndUpdateSelecteditems is called.
///
internal void BeginUpdateSelectedItems()
{
if (_selector.SelectionChange.IsActive || _updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.DeferSelectionActive));
}
_updatingSelectedItems = true;
_selector.SelectionChange.Begin();
}
///
/// Commit selection changes.
///
internal void EndUpdateSelectedItems()
{
if (!_selector.SelectionChange.IsActive || !_updatingSelectedItems)
{
throw new InvalidOperationException(SR.Get(SRID.DeferSelectionNotActive));
}
_updatingSelectedItems = false;
_selector.SelectionChange.End();
}
///
/// Returns true after BeginUpdateSelectedItems is called
///
internal bool IsUpdatingSelectedItems
{
get
{
return _selector.SelectionChange.IsActive || _updatingSelectedItems;
}
}
#endregion
#region Private data
// Keep a reference for Selector owner
private Selector _selector;
// We need a flag for indicating user bulk selection mode. We cannot re-use SelectionChange.IsActive because there are cases when SelectionChange.IsActive==true and SelectedItems.Add is called internally (End()) to update the collection
// When EndUpdateSelectedItems() is called we first reset this flag to allow SelectedItems.Add to change the collection
private bool _updatingSelectedItems;
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.