Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / DataGridColumnHeaderCollection.cs / 1305600 / DataGridColumnHeaderCollection.cs
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
namespace System.Windows.Controls
{
///
/// Collection used as the ItemsSource of The DataGridColumnHeadersPresenter. This is a wrapper around the Column collection; each item
/// returns the corresponding Column.Header.
///
internal class DataGridColumnHeaderCollection : IEnumerable, INotifyCollectionChanged, IDisposable
{
public DataGridColumnHeaderCollection(ObservableCollection columns)
{
_columns = columns;
if (_columns != null)
{
_columns.CollectionChanged += OnColumnsChanged;
}
}
public DataGridColumn ColumnFromIndex(int index)
{
if (index >= 0 && index < _columns.Count)
{
return _columns[index];
}
return null;
}
#region Notification Propagation
///
/// Called when the Header property on a Column changes. Causes us to fire a CollectionChanged event to specify that an item has been replaced.
///
///
internal void NotifyHeaderPropertyChanged(DataGridColumn column, DependencyPropertyChangedEventArgs e)
{
Debug.Assert(e.Property == DataGridColumn.HeaderProperty, "We only want to know about the header property changing");
Debug.Assert(_columns.Contains(column));
NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace,
e.NewValue,
e.OldValue,
_columns.IndexOf(column));
FireCollectionChanged(args);
}
#endregion
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
if (_columns != null)
{
_columns.CollectionChanged -= OnColumnsChanged;
}
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new ColumnHeaderCollectionEnumerator(_columns);
}
private class ColumnHeaderCollectionEnumerator : IEnumerator, IDisposable
{
public ColumnHeaderCollectionEnumerator(ObservableCollection columns)
{
if (columns != null)
{
_columns = columns;
_columns.CollectionChanged += OnColumnsChanged;
}
_current = -1;
}
#region IEnumerator Members
public object Current
{
get
{
if (IsValid)
{
DataGridColumn column = _columns[_current];
if (column != null)
{
return column.Header;
}
return null;
}
throw new InvalidOperationException();
}
}
public bool MoveNext()
{
if (HasChanged)
{
throw new InvalidOperationException();
}
if (_columns != null && _current < _columns.Count - 1)
{
_current++;
return true;
}
return false;
}
public void Reset()
{
if (HasChanged)
{
throw new InvalidOperationException();
}
_current = -1;
}
#endregion
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
if (_columns != null)
{
_columns.CollectionChanged -= OnColumnsChanged;
}
}
#endregion
#region Helpers
private bool HasChanged
{
get
{
return _columnsChanged;
}
}
private bool IsValid
{
get
{
return _columns != null && _current >= 0 && _current < _columns.Count && !HasChanged;
}
}
private void OnColumnsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
_columnsChanged = true;
}
#endregion
#region Data
private int _current;
private bool _columnsChanged;
private ObservableCollection _columns;
#endregion
}
#endregion
#region INotifyCollectionChanged Members
///
/// INotifyCollectionChanged CollectionChanged event
///
public event NotifyCollectionChangedEventHandler CollectionChanged;
///
/// Helper to raise a CollectionChanged event when the columns collection has changed
///
///
///
private void OnColumnsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventArgs newArgs;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.NewItems), e.NewStartingIndex);
break;
case NotifyCollectionChangedAction.Remove:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.OldItems), e.OldStartingIndex);
break;
case NotifyCollectionChangedAction.Move:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.OldItems), e.NewStartingIndex, e.OldStartingIndex);
break;
case NotifyCollectionChangedAction.Replace:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.NewItems), HeadersFromColumns(e.OldItems), e.OldStartingIndex);
break;
default:
newArgs = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
break;
}
FireCollectionChanged(newArgs);
}
private void FireCollectionChanged(NotifyCollectionChangedEventArgs args)
{
if (CollectionChanged != null)
{
CollectionChanged(this, args);
}
}
private static object[] HeadersFromColumns(IList columns)
{
object[] headers = new object[columns.Count];
for (int i = 0; i < columns.Count; i++)
{
DataGridColumn column = columns[i] as DataGridColumn;
if (column != null)
{
headers[i] = column.Header;
}
else
{
headers[i] = null;
}
}
return headers;
}
#endregion
private ObservableCollection _columns;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
namespace System.Windows.Controls
{
///
/// Collection used as the ItemsSource of The DataGridColumnHeadersPresenter. This is a wrapper around the Column collection; each item
/// returns the corresponding Column.Header.
///
internal class DataGridColumnHeaderCollection : IEnumerable, INotifyCollectionChanged, IDisposable
{
public DataGridColumnHeaderCollection(ObservableCollection columns)
{
_columns = columns;
if (_columns != null)
{
_columns.CollectionChanged += OnColumnsChanged;
}
}
public DataGridColumn ColumnFromIndex(int index)
{
if (index >= 0 && index < _columns.Count)
{
return _columns[index];
}
return null;
}
#region Notification Propagation
///
/// Called when the Header property on a Column changes. Causes us to fire a CollectionChanged event to specify that an item has been replaced.
///
///
internal void NotifyHeaderPropertyChanged(DataGridColumn column, DependencyPropertyChangedEventArgs e)
{
Debug.Assert(e.Property == DataGridColumn.HeaderProperty, "We only want to know about the header property changing");
Debug.Assert(_columns.Contains(column));
NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace,
e.NewValue,
e.OldValue,
_columns.IndexOf(column));
FireCollectionChanged(args);
}
#endregion
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
if (_columns != null)
{
_columns.CollectionChanged -= OnColumnsChanged;
}
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new ColumnHeaderCollectionEnumerator(_columns);
}
private class ColumnHeaderCollectionEnumerator : IEnumerator, IDisposable
{
public ColumnHeaderCollectionEnumerator(ObservableCollection columns)
{
if (columns != null)
{
_columns = columns;
_columns.CollectionChanged += OnColumnsChanged;
}
_current = -1;
}
#region IEnumerator Members
public object Current
{
get
{
if (IsValid)
{
DataGridColumn column = _columns[_current];
if (column != null)
{
return column.Header;
}
return null;
}
throw new InvalidOperationException();
}
}
public bool MoveNext()
{
if (HasChanged)
{
throw new InvalidOperationException();
}
if (_columns != null && _current < _columns.Count - 1)
{
_current++;
return true;
}
return false;
}
public void Reset()
{
if (HasChanged)
{
throw new InvalidOperationException();
}
_current = -1;
}
#endregion
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
if (_columns != null)
{
_columns.CollectionChanged -= OnColumnsChanged;
}
}
#endregion
#region Helpers
private bool HasChanged
{
get
{
return _columnsChanged;
}
}
private bool IsValid
{
get
{
return _columns != null && _current >= 0 && _current < _columns.Count && !HasChanged;
}
}
private void OnColumnsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
_columnsChanged = true;
}
#endregion
#region Data
private int _current;
private bool _columnsChanged;
private ObservableCollection _columns;
#endregion
}
#endregion
#region INotifyCollectionChanged Members
///
/// INotifyCollectionChanged CollectionChanged event
///
public event NotifyCollectionChangedEventHandler CollectionChanged;
///
/// Helper to raise a CollectionChanged event when the columns collection has changed
///
///
///
private void OnColumnsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventArgs newArgs;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.NewItems), e.NewStartingIndex);
break;
case NotifyCollectionChangedAction.Remove:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.OldItems), e.OldStartingIndex);
break;
case NotifyCollectionChangedAction.Move:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.OldItems), e.NewStartingIndex, e.OldStartingIndex);
break;
case NotifyCollectionChangedAction.Replace:
newArgs = new NotifyCollectionChangedEventArgs(e.Action, HeadersFromColumns(e.NewItems), HeadersFromColumns(e.OldItems), e.OldStartingIndex);
break;
default:
newArgs = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
break;
}
FireCollectionChanged(newArgs);
}
private void FireCollectionChanged(NotifyCollectionChangedEventArgs args)
{
if (CollectionChanged != null)
{
CollectionChanged(this, args);
}
}
private static object[] HeadersFromColumns(IList columns)
{
object[] headers = new object[columns.Count];
for (int i = 0; i < columns.Count; i++)
{
DataGridColumn column = columns[i] as DataGridColumn;
if (column != null)
{
headers[i] = column.Header;
}
else
{
headers[i] = null;
}
}
return headers;
}
#endregion
private ObservableCollection _columns;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- LogicalTreeHelper.cs
- QueryPageSettingsEventArgs.cs
- NewExpression.cs
- DataBoundControl.cs
- MatrixTransform3D.cs
- Compiler.cs
- RepeatButton.cs
- SiteMapNodeItemEventArgs.cs
- XmlSchemaDocumentation.cs
- templategroup.cs
- DbProviderFactories.cs
- MenuItemAutomationPeer.cs
- AudienceUriMode.cs
- SQLDateTime.cs
- control.ime.cs
- AsymmetricKeyExchangeDeformatter.cs
- SettingsBindableAttribute.cs
- BufferBuilder.cs
- SchemaCollectionPreprocessor.cs
- ConstantProjectedSlot.cs
- LinkArea.cs
- WeakReferenceList.cs
- PageThemeCodeDomTreeGenerator.cs
- FilteredDataSetHelper.cs
- PackageStore.cs
- NativeMethods.cs
- WsdlInspector.cs
- LinkLabelLinkClickedEvent.cs
- HashAlgorithm.cs
- XamlInt32CollectionSerializer.cs
- Update.cs
- CoreSwitches.cs
- IntegerFacetDescriptionElement.cs
- ClientSponsor.cs
- CharacterShapingProperties.cs
- PropertyBuilder.cs
- ButtonFieldBase.cs
- Configuration.cs
- PersianCalendar.cs
- VisualStyleRenderer.cs
- XmlSerializerAssemblyAttribute.cs
- RowCache.cs
- SpellCheck.cs
- TargetPerspective.cs
- SafeRightsManagementPubHandle.cs
- OperationCanceledException.cs
- Win32PrintDialog.cs
- EnlistmentTraceIdentifier.cs
- DataGridViewCellStyleChangedEventArgs.cs
- Rectangle.cs
- TabControlEvent.cs
- OracleTimeSpan.cs
- DesignRelation.cs
- ProcessInputEventArgs.cs
- Expression.cs
- PropVariant.cs
- BitmapSizeOptions.cs
- TextContainerChangedEventArgs.cs
- ListViewUpdateEventArgs.cs
- DrawingBrush.cs
- TcpServerChannel.cs
- GlobalizationAssembly.cs
- XmlEnumAttribute.cs
- _AutoWebProxyScriptEngine.cs
- ObjectDataSourceSelectingEventArgs.cs
- Cloud.cs
- HwndSubclass.cs
- PropertyEmitterBase.cs
- UIElementPropertyUndoUnit.cs
- Vector3DValueSerializer.cs
- HttpsHostedTransportConfiguration.cs
- ConnectionPool.cs
- MimeMultiPart.cs
- SystemIPv6InterfaceProperties.cs
- SqlRowUpdatedEvent.cs
- EUCJPEncoding.cs
- Metadata.cs
- ConstraintEnumerator.cs
- httpapplicationstate.cs
- ConnectionManagementElement.cs
- DependencySource.cs
- codemethodreferenceexpression.cs
- FrameworkElementFactory.cs
- Rotation3DAnimationUsingKeyFrames.cs
- SecurityPermission.cs
- documentsequencetextpointer.cs
- ShaderEffect.cs
- WindowsFormsSectionHandler.cs
- DelayedRegex.cs
- TemplatingOptionsDialog.cs
- ContentPathSegment.cs
- Matrix3DStack.cs
- TrackingValidationObjectDictionary.cs
- GridViewCancelEditEventArgs.cs
- DocumentViewer.cs
- RequestBringIntoViewEventArgs.cs
- XPathEmptyIterator.cs
- SystemResourceHost.cs
- PrivilegeNotHeldException.cs
- CodeCompiler.cs