private void RecursiveRestoreDataGridContextsState(DataGridContext dataGridContext) { WeakDataGridContextKey weakDataGridContextKey = new WeakDataGridContextKey(dataGridContext); SaveRestoreDataGridContextStateVisitor saveRestoreDataGridContextStateVisitor; if (m_dataGridContextsStateDictionary.TryGetValue(weakDataGridContextKey, out saveRestoreDataGridContextStateVisitor)) { try { saveRestoreDataGridContextStateVisitor.RestoreState(dataGridContext as IDataGridContextVisitable); } finally { m_dataGridContextsStateDictionary.Remove(weakDataGridContextKey); } } IEnumerable <DataGridContext> subDataGridContexts = dataGridContext.GetChildContexts(); foreach (DataGridContext subDataGridContext in subDataGridContexts) { this.RecursiveRestoreDataGridContextsState(subDataGridContext); } }
private static DataGridContext GetDataGridContextFromDetailConfiguration( DetailConfiguration configuration, DataGridContext parentDataGridContext) { if ((configuration == null) || (parentDataGridContext == null)) { return(null); } if (parentDataGridContext.SourceDetailConfiguration == configuration) { return(parentDataGridContext); } foreach (DataGridContext childContext in parentDataGridContext.GetChildContexts()) { DataGridContext foundContext = HierarchicalGroupByControlNode.GetDataGridContextFromDetailConfiguration(configuration, childContext); if (foundContext != null) { return(foundContext); } } return(null); }
private void RecursiveSaveDataGridContextsState( DataGridContext dataGridContext ) { SaveRestoreDataGridContextStateVisitor saveRestoreDataGridContextStateVisitor = new SaveRestoreDataGridContextStateVisitor( true, int.MaxValue, m_stopAtFirstCollapsedGroup ); saveRestoreDataGridContextStateVisitor.SaveState( dataGridContext as IDataGridContextVisitable ); m_dataGridContextsStateDictionary.Add( new WeakDataGridContextKey( dataGridContext ), saveRestoreDataGridContextStateVisitor ); IEnumerable<DataGridContext> subDataGridContexts = dataGridContext.GetChildContexts(); foreach( DataGridContext subDataGridContext in subDataGridContexts ) { this.RecursiveSaveDataGridContextsState( subDataGridContext ); } }
private void RecursiveSaveDataGridContextsState(DataGridContext dataGridContext) { SaveRestoreDataGridContextStateVisitor saveRestoreDataGridContextStateVisitor = new SaveRestoreDataGridContextStateVisitor(true, int.MaxValue, m_stopAtFirstCollapsedGroup); saveRestoreDataGridContextStateVisitor.SaveState(dataGridContext as IDataGridContextVisitable); m_dataGridContextsStateDictionary.Add(new WeakDataGridContextKey(dataGridContext), saveRestoreDataGridContextStateVisitor); IEnumerable <DataGridContext> subDataGridContexts = dataGridContext.GetChildContexts(); foreach (DataGridContext subDataGridContext in subDataGridContexts) { this.RecursiveSaveDataGridContextsState(subDataGridContext); } }
// Parse all the expanded details recursively private void GetVisiblePositionsForContextDetailLevel( DataGridContext dataGridContext, int desiredDetailLevel, HashSet <int> exportedColumnPositions) { int dataGridContextDetailLevel = dataGridContext.DetailLevel; // The detail level is too deep, return immediately if (dataGridContextDetailLevel > desiredDetailLevel) { return; } // The desired detail level is reached get the exportedColumnPositions if (dataGridContextDetailLevel == desiredDetailLevel) { DataGridContext parentDataGridContext = dataGridContext.ParentDataGridContext; if (parentDataGridContext == null) { this.GetVisibleColumnsVisiblePositionForDataGridContext(dataGridContext, exportedColumnPositions); } else { foreach (DataGridContext childContext in parentDataGridContext.GetChildContexts()) { if (this.GetVisibleColumnsVisiblePositionForDataGridContext(childContext, exportedColumnPositions)) { // All columns need to be exported, stop parsing child DataGridContexts break; } } } } else { // The detail level differs, parse the child contexts recursively foreach (DataGridContext childContext in dataGridContext.GetChildContexts()) { this.GetVisiblePositionsForContextDetailLevel(childContext, desiredDetailLevel, exportedColumnPositions); } } }
public void ProcessVisit( DataGridContext sourceContext, int minIndex, int maxIndex, IDataGridContextVisitor visitor, DataGridContextVisitorType visitorType, bool visitDetails, out bool visitWasStopped) { visitWasStopped = false; // This is used only for DataGridContextVisitorType.ItemsBlock int startSourceDataItemIndex = -1; int endSourceDataItemIndex = -1; if (minIndex < 0) { throw new ArgumentException("The minimum index must be greater than or equal to zero."); } if ((visitorType & DataGridContextVisitorType.DataGridContext) == DataGridContextVisitorType.DataGridContext) { visitor.Visit(sourceContext, ref visitWasStopped); if (visitWasStopped) { return; } } //Take a shortcut, if the visit is made only for contexts, and there is no child contexts //return right away. bool containsDetails = false; foreach (DataGridContext childContext in sourceContext.GetChildContexts()) { containsDetails = true; break; } bool processed = false; do { //resets the flag that indicates if the node was already processed processed = false; int itemCount = this.CurrentNode.ItemCount; //If the whole current node is below the minIndex, jump over it. if ((this.Index + (itemCount - 1)) < minIndex) { processed = true; } //when the index to visit exceeds the range defined, exit the loop. if (this.Index > maxIndex) { break; } int minForNode = Math.Max(0, minIndex - this.Index); // this will give the base offset within the node where to start the visitating! int maxForNode = Math.Min(itemCount - 1, maxIndex - this.Index); //this will five the max offset within this node to visit (protected against overlfow ) if (!processed) { HeadersFootersGeneratorNode headersNode = this.CurrentNode as HeadersFootersGeneratorNode; if (headersNode != null) { bool isHeaderFooter = (headersNode.Parent == null); //If the node is a Headers or Footers node AND the visitorType does not contain HeadersFooters if ((isHeaderFooter) && ((visitorType & DataGridContextVisitorType.HeadersFooters) == DataGridContextVisitorType.HeadersFooters)) { GeneratorNodeHelper.ProcessHeadersNodeVisit(headersNode, sourceContext, minForNode, maxForNode, visitor, ref visitWasStopped); } else if ((!isHeaderFooter) && ((visitorType & DataGridContextVisitorType.GroupHeadersFooters) == DataGridContextVisitorType.GroupHeadersFooters)) { GeneratorNodeHelper.ProcessHeadersNodeVisit(headersNode, sourceContext, minForNode, maxForNode, visitor, ref visitWasStopped); } processed = true; } } if (!processed) { ItemsGeneratorNode itemsNode = this.CurrentNode as ItemsGeneratorNode; if (itemsNode != null) { if ((visitorType & DataGridContextVisitorType.ItemsBlock) == DataGridContextVisitorType.ItemsBlock) { GeneratorNodeHelper.ProcessItemsNodeBlockVisit( itemsNode, sourceContext, minForNode, maxForNode, visitor, visitorType, visitDetails, containsDetails, m_sourceDataIndex, ref startSourceDataItemIndex, ref endSourceDataItemIndex, ref visitWasStopped); } else if (((visitDetails) && (containsDetails)) || ((visitorType & DataGridContextVisitorType.Items) == DataGridContextVisitorType.Items)) { GeneratorNodeHelper.ProcessItemsNodeVisit( itemsNode, sourceContext, minForNode, maxForNode, visitor, visitorType, visitDetails, m_sourceDataIndex, ref visitWasStopped); } processed = true; } } if (!processed) { GroupGeneratorNode groupNode = this.CurrentNode as GroupGeneratorNode; if (groupNode != null) { if ((visitorType & DataGridContextVisitorType.Groups) == DataGridContextVisitorType.Groups) { visitor.Visit( sourceContext, groupNode.CollectionViewGroup, groupNode.NamesTree, groupNode.Level, groupNode.IsExpanded, groupNode.IsComputedExpanded, ref visitWasStopped); } processed = true; } } if (!processed) { throw new DataGridInternalException(); } if (visitWasStopped) { break; } if (this.MoveToChild()) { continue; } if (this.MoveToFollowing()) { continue; } break; }while(true); //loop is controled by continue and break statements. if ((visitorType & DataGridContextVisitorType.ItemsBlock) == DataGridContextVisitorType.ItemsBlock) { if (startSourceDataItemIndex != -1) { bool stopVisit = false; visitor.Visit(sourceContext, startSourceDataItemIndex, endSourceDataItemIndex, ref stopVisit); visitWasStopped |= stopVisit; } } }
private void EnsureNodeTreeCreatedOnAllSubContext( DataGridContext context ) { // context.GetChildContexts() do a EnsureNodeTreeCreated(), no need to do one before calling it foreach( var subContext in context.GetChildContexts() ) { this.EnsureNodeTreeCreatedOnAllSubContext( subContext ); } }
// Parse all the expanded details recursively private void GetVisiblePositionsForContextDetailLevel( DataGridContext dataGridContext, int desiredDetailLevel, HashSet<int> exportedColumnPositions ) { int dataGridContextDetailLevel = dataGridContext.DetailLevel; // The detail level is too deep, return immediately if( dataGridContextDetailLevel > desiredDetailLevel ) return; // The desired detail level is reached get the exportedColumnPositions if( dataGridContextDetailLevel == desiredDetailLevel ) { DataGridContext parentDataGridContext = dataGridContext.ParentDataGridContext; if( parentDataGridContext == null ) { this.GetVisibleColumnsVisiblePositionForDataGridContext( dataGridContext, exportedColumnPositions ); } else { foreach( DataGridContext childContext in parentDataGridContext.GetChildContexts() ) { if( this.GetVisibleColumnsVisiblePositionForDataGridContext( childContext, exportedColumnPositions ) ) { // All columns need to be exported, stop parsing child DataGridContexts break; } } } } else { // The detail level differs, parse the child contexts recursively foreach( DataGridContext childContext in dataGridContext.GetChildContexts() ) { this.GetVisiblePositionsForContextDetailLevel( childContext, desiredDetailLevel, exportedColumnPositions ); } } }
private static DataGridContext GetDataGridContextFromDetailConfiguration( DetailConfiguration configuration, DataGridContext parentDataGridContext ) { if( ( configuration == null ) || ( parentDataGridContext == null ) ) return null; if( parentDataGridContext.SourceDetailConfiguration == configuration ) return parentDataGridContext; foreach( DataGridContext childContext in parentDataGridContext.GetChildContexts() ) { DataGridContext foundContext = HierarchicalGroupByControlNode.GetDataGridContextFromDetailConfiguration( configuration, childContext ); if( foundContext != null ) return foundContext; } return null; }
private DataGridContext UpdateCurrentContextRecursive( DataGridContext parentDataGridContext, DataGridContext oldCurrentDataGridContext, out object newCurrentItem ) { if( parentDataGridContext == null ) { newCurrentItem = null; return null; } foreach( DataGridContext childContext in parentDataGridContext.GetChildContexts() ) { if( childContext.SourceDetailConfiguration == oldCurrentDataGridContext.SourceDetailConfiguration ) { object oldCurrentItem = oldCurrentDataGridContext.CurrentItem; System.Data.DataView dataView = ItemsSourceHelper.TryGetDataViewFromDataGridContext( childContext ); System.Data.DataRowView oldCurrentDataRowView = oldCurrentItem as System.Data.DataRowView; if( ( dataView != null ) && ( oldCurrentDataRowView != null ) ) { System.Data.DataRow oldDataRow = oldCurrentDataRowView.Row; foreach( System.Data.DataRowView dataRowView in dataView ) { if( dataRowView.Row == oldDataRow ) { newCurrentItem = dataRowView; return childContext; } } } else { if( childContext.Items.Contains( oldCurrentItem ) ) { newCurrentItem = oldCurrentItem; return childContext; } } } DataGridContext foundContext = this.UpdateCurrentContextRecursive( childContext, oldCurrentDataGridContext, out newCurrentItem ); if( foundContext != null ) return foundContext; } newCurrentItem = null; return null; }
internal static void ToggleColumnSort( DataGridContext dataGridContext, SortDescriptionCollection sortDescriptions, ColumnCollection columns, ColumnBase column, bool shiftUnpressed) { if (column == null) { throw new ArgumentNullException("column"); } Debug.Assert((dataGridContext != null) || (column.ParentDetailConfiguration != null)); if ((dataGridContext == null) && (column.ParentDetailConfiguration == null)) { throw new DataGridInternalException("DataGridContext or ParentDetailConfiguration can't be null."); } DataGridContext parentDataGridContext = (dataGridContext == null) ? null : dataGridContext.ParentDataGridContext; // Defer the RestoreState of each DataGridContext of the same level // to ensure all the DataGridContext will be correctly restored once // all of them are completely resorted HashSet <IDisposable> deferRestoreStateDisposable = new HashSet <IDisposable>(); if (parentDataGridContext != null) { foreach (DataGridContext childContext in parentDataGridContext.GetChildContexts()) { deferRestoreStateDisposable.Add(childContext.DeferRestoreState()); } } IDisposable deferResortHelper = (dataGridContext == null) ? null : SortingHelper.DeferResortHelper(dataGridContext.ItemsSourceCollection, dataGridContext.Items, sortDescriptions); //this will ensure that all DataGridCollectionViews mapped to this SortDescriptions collection will only refresh their sorting once! SortDirection newSortDirection = column.SortDirection; if ((shiftUnpressed) && ((column.SortIndex == -1) || (sortDescriptions.Count > 1))) { sortDescriptions.Clear(); } switch (newSortDirection) { case SortDirection.None: newSortDirection = SortDirection.Ascending; break; case SortDirection.Ascending: newSortDirection = SortDirection.Descending; break; case SortDirection.Descending: newSortDirection = SortDirection.None; break; } SortingHelper.ApplyColumnSort(dataGridContext, sortDescriptions, columns, column, newSortDirection); if (deferResortHelper != null) { //end of the DeferResort(), any DataGridCollectionView listening to the SortDescriptions instance will refresh its sorting! deferResortHelper.Dispose(); } foreach (IDisposable disposable in deferRestoreStateDisposable) { try { // Try/Catch to ensure all contexts are restored disposable.Dispose(); } catch (Exception) { } } deferRestoreStateDisposable.Clear(); }
public void ProcessVisit( DataGridContext sourceContext, int minIndex, int maxIndex, IDataGridContextVisitor visitor, DataGridContextVisitorType visitorType, bool visitDetails, out bool visitWasStopped ) { visitWasStopped = false; // This is used only for DataGridContextVisitorType.ItemsBlock int startSourceDataItemIndex = -1; int endSourceDataItemIndex = -1; if( minIndex < 0 ) { DataGridException.ThrowSystemException( "The minimum index must be greater than or equal to zero.", typeof( ArgumentException ), sourceContext.DataGridControl.Name, "minIndex" ); } if( ( visitorType & DataGridContextVisitorType.DataGridContext ) == DataGridContextVisitorType.DataGridContext ) { visitor.Visit( sourceContext, ref visitWasStopped ); if( visitWasStopped ) return; } //Take a shortcut, if the visit is made only for contexts, and there is no child contexts //return right away. bool containsDetails = false; foreach( DataGridContext childContext in sourceContext.GetChildContexts() ) { containsDetails = true; break; } bool processed = false; do { //resets the flag that indicates if the node was already processed processed = false; int itemCount = this.CurrentNode.ItemCount; //If the whole current node is below the minIndex, jump over it. if( ( this.Index + ( itemCount - 1 ) ) < minIndex ) { processed = true; } //when the index to visit exceeds the range defined, exit the loop. if( this.Index > maxIndex ) break; int minForNode = Math.Max( 0, minIndex - this.Index ); // this will give the base offset within the node where to start the visitating! int maxForNode = Math.Min( itemCount - 1, maxIndex - this.Index ); //this will five the max offset within this node to visit (protected against overlfow ) if( !processed ) { HeadersFootersGeneratorNode headersNode = this.CurrentNode as HeadersFootersGeneratorNode; if( headersNode != null ) { bool isHeaderFooter = ( headersNode.Parent == null ); //If the node is a Headers or Footers node AND the visitorType does not contain HeadersFooters if( ( isHeaderFooter ) && ( ( visitorType & DataGridContextVisitorType.HeadersFooters ) == DataGridContextVisitorType.HeadersFooters ) ) { GeneratorNodeHelper.ProcessHeadersNodeVisit( headersNode, sourceContext, minForNode, maxForNode, visitor, ref visitWasStopped ); } else if( ( !isHeaderFooter ) && ( ( visitorType & DataGridContextVisitorType.GroupHeadersFooters ) == DataGridContextVisitorType.GroupHeadersFooters ) ) { GeneratorNodeHelper.ProcessHeadersNodeVisit( headersNode, sourceContext, minForNode, maxForNode, visitor, ref visitWasStopped ); } processed = true; } } if( !processed ) { ItemsGeneratorNode itemsNode = this.CurrentNode as ItemsGeneratorNode; if( itemsNode != null ) { if( ( visitorType & DataGridContextVisitorType.ItemsBlock ) == DataGridContextVisitorType.ItemsBlock ) { GeneratorNodeHelper.ProcessItemsNodeBlockVisit( itemsNode, sourceContext, minForNode, maxForNode, visitor, visitorType, visitDetails, containsDetails, m_sourceDataIndex, ref startSourceDataItemIndex, ref endSourceDataItemIndex, ref visitWasStopped ); } else if( ( ( visitDetails ) && ( containsDetails ) ) || ( ( visitorType & DataGridContextVisitorType.Items ) == DataGridContextVisitorType.Items ) ) { GeneratorNodeHelper.ProcessItemsNodeVisit( itemsNode, sourceContext, minForNode, maxForNode, visitor, visitorType, visitDetails, m_sourceDataIndex, ref visitWasStopped ); } processed = true; } } if( !processed ) { GroupGeneratorNode groupNode = this.CurrentNode as GroupGeneratorNode; if( groupNode != null ) { if( ( visitorType & DataGridContextVisitorType.Groups ) == DataGridContextVisitorType.Groups ) { visitor.Visit( sourceContext, groupNode.CollectionViewGroup, groupNode.NamesTree, groupNode.Level, groupNode.IsExpanded, groupNode.IsComputedExpanded, ref visitWasStopped ); } processed = true; } } if( !processed ) throw new DataGridInternalException( "Unable to process the visit.", sourceContext.DataGridControl ); if( visitWasStopped ) break; if( this.MoveToChild() ) continue; if( this.MoveToFollowing() ) continue; break; } while( true ); //loop is controled by continue and break statements. if( ( visitorType & DataGridContextVisitorType.ItemsBlock ) == DataGridContextVisitorType.ItemsBlock ) { if( startSourceDataItemIndex != -1 ) { bool stopVisit = false; visitor.Visit( sourceContext, startSourceDataItemIndex, endSourceDataItemIndex, ref stopVisit ); visitWasStopped = visitWasStopped || stopVisit; } } }
private void RecursiveRestoreDataGridContextsState( DataGridContext dataGridContext ) { WeakDataGridContextKey weakDataGridContextKey = new WeakDataGridContextKey( dataGridContext ); SaveRestoreDataGridContextStateVisitor saveRestoreDataGridContextStateVisitor; if( m_dataGridContextsStateDictionary.TryGetValue( weakDataGridContextKey, out saveRestoreDataGridContextStateVisitor ) ) { try { saveRestoreDataGridContextStateVisitor.RestoreState( dataGridContext as IDataGridContextVisitable ); } finally { m_dataGridContextsStateDictionary.Remove( weakDataGridContextKey ); } } IEnumerable<DataGridContext> subDataGridContexts = dataGridContext.GetChildContexts(); foreach( DataGridContext subDataGridContext in subDataGridContexts ) { this.RecursiveRestoreDataGridContextsState( subDataGridContext ); } }