/// <summary> /// Updates the selected <see cref="DataGridColumnHeader"/>. /// </summary> /// <param name="datagrid">The datagrid.</param> private static void UpdateSelectedHeaders(DataGridControl datagrid) { if (null == datagrid) { return; } // Get the list of headers IList <DependencyObject> headers = VisualTreeHelperExtended.GetAllDescendants(datagrid, typeof(DataGridColumnHeader)); if (null == headers || 0 == headers.Count) { return; } // Update the selection based on the current tracking modes SelectionTrackingModes trackingModes = GetTrackingModes(datagrid); if (0 != (trackingModes & SelectionTrackingModes.Headers)) { // Update header selections, if any foreach (DataGridColumnHeader header in headers) { // Determine if the column associated with this header has any selected cells bool isSelected = false; foreach (DataGridCellInfo cellInfo in datagrid.SelectedCells) { if (cellInfo.Column == header.Column) { isSelected = true; break; } } // Update the selection for this header if (isSelected != GetIsSelectedHeader(header)) { if (isSelected) { header.SetValue(IsSelectedHeaderPropertyKey, true); } else { header.ClearValue(IsSelectedHeaderPropertyKey); } } } } else { // Clear header selections, if any foreach (DataGridColumnHeader header in headers) { if (GetIsSelectedHeader(header)) { header.ClearValue(IsSelectedHeaderPropertyKey); } } } }
public static void SetTrackingModes(DataGridControl obj, SelectionTrackingModes value) { if (null == obj) { throw new ArgumentNullException("obj"); } obj.SetValue(SelectionBehavior.TrackingModesProperty, value); }
/// <summary> /// Called when <see cref="TrackingModesProperty"/> is changed. /// </summary> /// <param name="d">The dependency object that was changed.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void OnTrackingModesPropertyValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataGridControl datagrid = d as DataGridControl; if (null == datagrid) { return; } SelectionTrackingModes trackingModes = (SelectionTrackingModes)e.NewValue; if (SelectionTrackingModes.None != trackingModes) { datagrid.SelectedCellsChanged += new SelectedCellsChangedEventHandler(OnDatagridSelectedCellsChanged); } else { datagrid.SelectedCellsChanged -= new SelectedCellsChangedEventHandler(OnDatagridSelectedCellsChanged); } UpdateSelectedHeaders(datagrid); }