private void ClearChangeIndicator() { DataGridAutomationPeer automationPeer = (DataGridAutomationPeer)DataGridAutomationPeer.CreatePeerForElement(NodeMetadataDataGrid); // Get the DataGridRowsPresenterAutomationPeer so we can find the rows in the data grid... DataGridRowsPresenterAutomationPeer dataGridRowsPresenterAutomationPeer = automationPeer.GetChildren(). Where(a => (a is DataGridRowsPresenterAutomationPeer)). Select(a => (a as DataGridRowsPresenterAutomationPeer)). FirstOrDefault(); if (null != dataGridRowsPresenterAutomationPeer) { foreach (var item in dataGridRowsPresenterAutomationPeer.GetChildren()) { // loop to find the DataGridCellAutomationPeer from which we can interrogate the owner -- which is a DataGridRow foreach (var subitem in (item as DataGridItemAutomationPeer).GetChildren()) { if ((subitem is DataGridCellAutomationPeer)) { // At last -- the only public method for finding a row.... DataGridRow row = DataGridRow.GetRowContainingElement(((subitem as DataGridCellAutomationPeer).Owner as FrameworkElement)); row.Foreground = new SolidColorBrush(Colors.Black); } } } } }
/// <summary> /// Scrolls a DataGridRow into view. /// </summary> /// <param name="itemsControl">The DataGrid containing the row.</param> /// <param name="itemContainer">The DataGridRow object.</param> protected override void ScrollIntoView(DataGrid itemsControl, DataGridRow itemContainer) { IScrollProvider scrollInfo = (IScrollProvider)DataGridAutomationPeer.CreatePeerForElement(itemsControl); double horizontalScrollPercentBeforeScroll = scrollInfo.HorizontalScrollPercent; double verticalScrollPercentBeforeScroll = scrollInfo.VerticalScrollPercent; itemsControl.ScrollIntoView(itemContainer.DataContext, null); double horizontalScrollPercentAfterScroll = scrollInfo.HorizontalScrollPercent; double verticalScrollPercentAfterScroll = scrollInfo.VerticalScrollPercent; double horizontalDiff = horizontalScrollPercentAfterScroll - horizontalScrollPercentBeforeScroll; double verticalDiff = verticalScrollPercentAfterScroll - verticalScrollPercentBeforeScroll; scrollInfo.SetScrollPercent(scrollInfo.HorizontalScrollPercent + (horizontalDiff / 2.0), scrollInfo.VerticalScrollPercent + (verticalDiff / 2.0)); }
static void EnsureRowVisible(DataGrid grid, int itemIndex) { grid.ScrollIntoView(grid.Items[itemIndex]); grid.UpdateLayout(); if (grid.ItemContainerGenerator.ContainerFromIndex(itemIndex) == null) { DataGridAutomationPeer peer = new DataGridAutomationPeer(grid); var scroll = peer.GetPattern(PatternInterface.Scroll) as IScrollProvider; scroll.SetScrollPercent(scroll.HorizontalScrollPercent, 0); grid.UpdateLayout(); while (grid.ItemContainerGenerator.ContainerFromIndex(itemIndex) == null) { scroll.Scroll(ScrollAmount.NoAmount, ScrollAmount.LargeIncrement); grid.UpdateLayout(); } } }
public DataGridRow GetDataGridRowByDataContext(DataGrid dataGrid, object dataContext) { if (null != dataContext) { DataGridAutomationPeer automationPeer = (DataGridAutomationPeer)DataGridAutomationPeer.CreatePeerForElement(dataGrid); // Get the DataGridRowsPresenterAutomationPeer so we can find the rows in the data grid... DataGridRowsPresenterAutomationPeer dataGridRowsPresenterAutomationPeer = automationPeer.GetChildren(). Where(a => (a is DataGridRowsPresenterAutomationPeer)). Select(a => (a as DataGridRowsPresenterAutomationPeer)). FirstOrDefault(); if (null != dataGridRowsPresenterAutomationPeer) { foreach (var item in dataGridRowsPresenterAutomationPeer.GetChildren()) { // loop to find the DataGridCellAutomationPeer from which we can interrogate the owner -- which is a DataGridRow foreach (var subitem in (item as DataGridItemAutomationPeer).GetChildren()) { if ((subitem is DataGridCellAutomationPeer)) { // At last -- the only public method for finding a row.... DataGridRow row = DataGridRow.GetRowContainingElement(((subitem as DataGridCellAutomationPeer).Owner as FrameworkElement)); // check this row to see if it is bound to the requested dataContext. if ((row.DataContext) == dataContext) { return(row); } break; // Only need to check one cell in each row } } } } } return(null); }
private static void OnIsSelectedChanged(object sender, DependencyPropertyChangedEventArgs e) { DataGridRow row = (DataGridRow)sender; bool isSelected = (bool)e.NewValue; if (isSelected && !row.IsSelectable) { throw new InvalidOperationException(SR.Get(SRID.DataGridRow_CannotSelectRowWhenCells)); } DataGrid grid = row.DataGridOwner; if (grid != null && row.DataContext != null) { DataGridAutomationPeer gridPeer = UIElementAutomationPeer.FromElement(grid) as DataGridAutomationPeer; if (gridPeer != null) { DataGridItemAutomationPeer rowItemPeer = gridPeer.GetOrCreateItemPeer(row.DataContext); if (rowItemPeer != null) { rowItemPeer.RaisePropertyChangedEvent( System.Windows.Automation.SelectionItemPatternIdentifiers.IsSelectedProperty, (bool)e.OldValue, isSelected); } } } // Update the header's IsRowSelected property row.NotifyPropertyChanged(row, e, NotificationTarget.Rows | NotificationTarget.RowHeaders); // This will raise the appropriate selection event, which will // bubble to the DataGrid. The base class Selector code will listen // for these events and will update SelectedItems as necessary. row.RaiseSelectionChangedEvent(isSelected); }
private void ProcessSort() { if (this.OwningColumn != null && this.OwningGrid != null && this.OwningGrid.EditingRow == null && this.OwningColumn != this.OwningGrid.ColumnsInternal.FillerColumn && this.OwningGrid.CanUserSortColumns && this.OwningColumn.CanUserSort) { DataGridColumnEventArgs ea = new DataGridColumnEventArgs(this.OwningColumn); this.OwningGrid.OnColumnSorting(ea); #if FEATURE_ICOLLECTIONVIEW_SORT if (!ea.Handled && this.OwningGrid.DataConnection.AllowSort && this.OwningGrid.DataConnection.SortDescriptions != null) { // - DataConnection.AllowSort is true, and // - SortDescriptionsCollection exists, and // - the column's data type is comparable DataGrid owningGrid = this.OwningGrid; ListSortDirection newSortDirection; SortDescription newSort; bool ctrl; bool shift; KeyboardHelper.GetMetaKeyState(out ctrl, out shift); SortDescription?sort = this.OwningColumn.GetSortDescription(); ICollectionView collectionView = owningGrid.DataConnection.CollectionView; Debug.Assert(collectionView != null); try { owningGrid.OnUserSorting(); using (collectionView.DeferRefresh()) { // If shift is held down, we multi-sort, therefore if it isn't, we'll clear the sorts beforehand if (!shift || owningGrid.DataConnection.SortDescriptions.Count == 0) { if (collectionView.CanGroup && collectionView.GroupDescriptions != null) { // Make sure we sort by the GroupDescriptions first for (int i = 0; i < collectionView.GroupDescriptions.Count; i++) { PropertyGroupDescription groupDescription = collectionView.GroupDescriptions[i] as PropertyGroupDescription; if (groupDescription != null && collectionView.SortDescriptions.Count <= i || collectionView.SortDescriptions[i].PropertyName != groupDescription.PropertyName) { collectionView.SortDescriptions.Insert(Math.Min(i, collectionView.SortDescriptions.Count), new SortDescription(groupDescription.PropertyName, ListSortDirection.Ascending)); } } while (collectionView.SortDescriptions.Count > collectionView.GroupDescriptions.Count) { collectionView.SortDescriptions.RemoveAt(collectionView.GroupDescriptions.Count); } } else if (!shift) { owningGrid.DataConnection.SortDescriptions.Clear(); } } if (sort.HasValue) { // swap direction switch (sort.Value.Direction) { case ListSortDirection.Ascending: newSortDirection = ListSortDirection.Descending; break; default: newSortDirection = ListSortDirection.Ascending; break; } newSort = new SortDescription(sort.Value.PropertyName, newSortDirection); // changing direction should not affect sort order, so we replace this column's // sort description instead of just adding it to the end of the collection int oldIndex = owningGrid.DataConnection.SortDescriptions.IndexOf(sort.Value); if (oldIndex >= 0) { owningGrid.DataConnection.SortDescriptions.Remove(sort.Value); owningGrid.DataConnection.SortDescriptions.Insert(oldIndex, newSort); } else { owningGrid.DataConnection.SortDescriptions.Add(newSort); } } else { // start new sort newSortDirection = ListSortDirection.Ascending; string propertyName = this.OwningColumn.GetSortPropertyName(); // no-opt if we couldn't find a property to sort on if (string.IsNullOrEmpty(propertyName)) { return; } newSort = new SortDescription(propertyName, newSortDirection); owningGrid.DataConnection.SortDescriptions.Add(newSort); } } } finally { owningGrid.OnUserSorted(); } sortProcessed = true; } #endif // Send the Invoked event for the column header's automation peer. DataGridAutomationPeer.RaiseAutomationInvokeEvent(this); } }