/class/Microsoft.SilverlightControls/SDKControls/Data/src/DataGrid/DataGrid.xaml.cs
C# | 6063 lines | 4566 code | 784 blank | 713 comment | 1160 complexity | ed3fbb8e759d43c44a097ec2760d700b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
Large files files are truncated, but you can click here to view the full file
- // (c) Copyright Microsoft Corporation.
- // This source is subject to the Microsoft Public License (Ms-PL).
- // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
- // All other rights reserved.
-
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using System.Windows.Automation.Peers;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
-
- namespace System.Windows.Controls
- {
- [SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
- /// <summary>
- /// Displays data in a customizable grid.
- /// </summary>
- [TemplatePart(Name = DataGrid.DATAGRID_elementRowsPresenterName, Type = typeof(DataGridRowsPresenter))]
- [TemplatePart(Name = DataGrid.DATAGRID_elementColumnHeadersPresenterName, Type = typeof(DataGridColumnHeadersPresenter))]
- [TemplatePart(Name = DataGrid.DATAGRID_elementFrozenColumnScrollBarSpacerName, Type = typeof(FrameworkElement))]
- [TemplatePart(Name = DataGrid.DATAGRID_elementHorizontalScrollbarName, Type = typeof(ScrollBar))]
- [TemplatePart(Name = DataGrid.DATAGRID_elementVerticalScrollbarName, Type = typeof(ScrollBar))]
- public partial class DataGrid : Control
- {
- #region Constants
-
- private const string DATAGRID_elementRowsPresenterName = "RowsPresenter";
- private const string DATAGRID_elementColumnHeadersPresenterName = "ColumnHeadersPresenter";
- private const string DATAGRID_elementFrozenColumnScrollBarSpacerName = "FrozenColumnScrollBarSpacer";
- private const string DATAGRID_elementHorizontalScrollbarName = "HorizontalScrollbar";
- private const string DATAGRID_elementRowHeadersPresenterName = "RowHeadersPresenter";
- private const string DATAGRID_elementTopLeftCornerHeaderName = "TopLeftCornerHeader";
- private const string DATAGRID_elementTopRightCornerHeaderName = "TopRightCornerHeader";
- private const string DATAGRID_elementVerticalScrollbarName = "VerticalScrollbar";
-
- private const bool DATAGRID_defaultAutoGenerateColumns = true;
- internal const bool DATAGRID_defaultCanUserReorderColumns = true;
- internal const bool DATAGRID_defaultCanUserResizeColumns = true;
- internal const bool DATAGRID_defaultCanUserSortColumns = true;
- private const DataGridRowDetailsVisibilityMode DATAGRID_defaultRowDetailsVisibility = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
- private const DataGridSelectionMode DATAGRID_defaultSelectionMode = DataGridSelectionMode.Extended;
-
- private const double DATAGRID_horizontalGridLinesThickness = 1;
- private const double DATAGRID_minimumRowHeaderWidth = 4;
- private const double DATAGRID_minimumColumnHeaderHeight = 4;
- private const double DATAGRID_maxHeadersThickness = 32768;
-
- private const double DATAGRID_defaultRowHeight = 22;
- private const double DATAGRID_defaultMinColumnWidth = 20;
- private const double DATAGRID_defaultMaxColumnWidth = double.PositiveInfinity;
-
- #endregion Constants
-
- #region Data
-
- // DataGrid Template Parts
- private DataGridRowsPresenter _rowsPresenter;
- private DataGridColumnHeadersPresenter _columnHeadersPresenter;
- private ScrollBar _hScrollBar;
- private ScrollBar _vScrollBar;
-
- private List<object> _addedSelectedItems;
- private byte _autoGeneratingColumnOperationCount;
- private Popup _columnDropLocationIndicatorPopup;
- private Control _columnDropLocationIndicator;
- private DataGridCellCoordinates _currentCellCoordinates;
- private List<DataGridCell> _editingBoundCells;
- private int _editingElementGotFocusListeners;
- private int _editingElementLostFocusListeners; // Number of subscribers for the LostFocus event of the element of a edited cell in a column
- private int _editingColumnIndex;
- private DataGridRow _editingRow;
- private RoutedEventArgs _editingEventArgs;
- private Control _editingTemplateControl; // Control that has focus inside a template column
- private bool _focusEditingControl;
- private DataGridRow _focusedRow;
- private FrameworkElement _frozenColumnScrollBarSpacer;
- // the sum of the widths in pixels of the scrolling columns preceding
- // the first displayed scrolling column
- private double _horizontalOffset;
- private bool _ignoreNextScrollBarsLayout;
- private byte _horizontalScrollChangesIgnored;
- // Nth row of rows 0..N that make up the RowHeightEstimate
- private int _lastEstimatedRow;
- private List<DataGridRow> _loadedRows;
- // prevents reentry into the VerticalScroll event handler
- private bool _makeFirstDisplayedCellCurrentCellPending;
- private bool _measured;
- private int? _mouseOverRowIndex; // -1 is used for the 'new row'
- // the number of pixels of the firstDisplayedScrollingCol which are not displayed
- private double _negHorizontalOffset;
- // the number of pixels of DisplayData.FirstDisplayedScrollingRow which are not displayed
- //
-
-
- private int _noSelectionChangeCount;
- private List<object> _removedSelectedItems;
- private double _rowHeaderDesiredWidth;
- private DataGridSelectedItemsCollection _selectedItems;
- private bool _selectionChanged;
- private IndexToValueTable<Visibility> _showDetailsTable;
- private bool _temporarilyResetCurrentCell;
- private ContentControl _topLeftCornerHeader;
- private ContentControl _topRightCornerHeader;
- private object _uneditedValue; // Represents the original current cell value at the time it enters editing mode.
- private byte _verticalScrollChangesIgnored;
-
- // An approximation of the sum of the heights in pixels of the scrolling rows preceding
- // the first displayed scrolling row. Since the scrolled off rows are discarded, the grid
- // does not know their actual height. The heights used for the approximation are the ones
- // set as the rows were scrolled off.
- private double _verticalOffset;
-
- #endregion Data
-
- #region Events
-
- public event EventHandler<DataGridAutoGeneratingColumnEventArgs> AutoGeneratingColumn;
- public event EventHandler<DataGridBeginningEditEventArgs> BeginningEdit;
- //
-
- public event EventHandler<DataGridColumnEventArgs> ColumnDisplayIndexChanged;
- public event EventHandler<DragStartedEventArgs> ColumnHeaderDragStarted;
- public event EventHandler<DragDeltaEventArgs> ColumnHeaderDragDelta;
- public event EventHandler<DragCompletedEventArgs> ColumnHeaderDragCompleted;
- /// <summary>
- /// Raised when column reordering ends, to allow subscribers to clean up.
- /// </summary>
- public event EventHandler<DataGridColumnEventArgs> ColumnReordered;
- /// <summary>
- /// Raised when starting a column reordering action. Subscribers to this event can
- /// set tooltip and caret UIElements, constrain tooltip position, indicate that
- /// a preview should be shown, or cancel reordering.
- /// </summary>
- public event EventHandler<DataGridColumnReorderingEventArgs> ColumnReordering;
- //
-
- public event EventHandler<EventArgs> CurrentCellChanged;
- public event EventHandler<DataGridPreparingCellForEditEventArgs> PreparingCellForEdit;
- public event EventHandler<DataGridRowEventArgs> LoadingRow;
- public event EventHandler<DataGridRowDetailsEventArgs> LoadingRowDetails;
- public event EventHandler<DataGridRowEventArgs> UnloadingRow;
- public event EventHandler<DataGridRowDetailsEventArgs> UnloadingRowDetails;
- public event EventHandler<DataGridRowDetailsEventArgs> RowDetailsVisibilityChanged;
- public event SelectionChangedEventHandler SelectionChanged;
-
- #endregion Events
-
- /// <summary>
- /// Initializes a new instance of the DataGrid class.
- /// </summary>
- [SuppressMessage("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily", Justification="_minRowHeight should be 0.")]
- public DataGrid()
- {
- this.TabNavigation = KeyboardNavigationMode.Once;
- this.KeyDown += new KeyEventHandler(DataGrid_KeyDown);
- this.KeyUp += new KeyEventHandler(DataGrid_KeyUp);
- this.GotFocus += new RoutedEventHandler(DataGrid_GotFocus);
- this.LostFocus += new RoutedEventHandler(DataGrid_LostFocus);
-
- this._loadedRows = new List<DataGridRow>();
- this._editingBoundCells = new List<DataGridCell>(2);
- this._selectedItems = new DataGridSelectedItemsCollection(this);
- this.SetValueNoCallback(SelectedIndexProperty, -1);
-
- this.DisplayData = new DataGridDisplayData();
- this.ColumnsInternal = CreateColumnsInstance();
-
- this.SetValueNoCallback(ColumnWidthProperty, DataGridLength.Auto);
- this.SetValueNoCallback(MaxColumnWidthProperty, DATAGRID_defaultMaxColumnWidth);
- this.SetValueNoCallback(MinColumnWidthProperty, DATAGRID_defaultMinColumnWidth);
- this.SetValueNoCallback(RowHeightProperty, double.NaN);
- this.RowHeightEstimate = DATAGRID_defaultRowHeight;
- this.RowDetailsHeightEstimate = 0;
- this._rowHeaderDesiredWidth = 0;
-
- this.DataConnection = new DataGridDataConnection(this);
- //this._newRowLocation = DataGridNewRowLocation.Inline;
- this._showDetailsTable = new IndexToValueTable<Visibility>();
-
- this.AnchorRowIndex = -1;
- this._lastEstimatedRow = -1;
- this._editingColumnIndex = -1;
- this._mouseOverRowIndex = null;
- this.CurrentCellCoordinates = new DataGridCellCoordinates(-1, -1);
-
- this.SetValueNoCallback(RowHeaderWidthProperty, double.NaN);
- this.SetValueNoCallback(ColumnHeaderHeightProperty, double.NaN);
-
- this._addedSelectedItems = new List<object>();
- this._removedSelectedItems = new List<object>();
-
- DefaultStyleKey = typeof(DataGrid);
- }
-
- #region Dependency Properties
-
- #region AlternatingRowBackground
-
- /// <summary>
- /// Gets or sets a brush that describes the background of odd-numbered rows in the grid.
- /// </summary>
- public Brush AlternatingRowBackground
- {
- get { return GetValue(AlternatingRowBackgroundProperty) as Brush; }
- set { SetValue(AlternatingRowBackgroundProperty, value); }
- }
-
- /// <summary>
- /// Identifies the AlternatingRowBackground dependency property.
- /// </summary>
- public static readonly DependencyProperty AlternatingRowBackgroundProperty =
- DependencyProperty.Register(
- "AlternatingRowBackground",
- typeof(Brush),
- typeof(DataGrid),
- new PropertyMetadata(OnAlternatingRowBackgroundPropertyChanged));
-
- private static void OnAlternatingRowBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (dataGrid._rowsPresenter != null)
- {
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- row.EnsureBackground();
- }
- }
- }
- #endregion AlternatingRowBackground
-
- #region AreRowDetailsFrozen
- /// <summary>
- /// Gets or sets a value indicating whether the horizontal ScrollBar of the DataGrid affects the
- /// details section of a row.
- /// </summary>
- public bool AreRowDetailsFrozen
- {
- get { return (bool)GetValue(AreRowDetailsFrozenProperty); }
- set { SetValue(AreRowDetailsFrozenProperty, value); }
- }
-
- /// <summary>
- /// Identifies the AreRowDetailsFrozen dependency property.
- /// </summary>
- public static readonly DependencyProperty AreRowDetailsFrozenProperty =
- DependencyProperty.Register(
- "AreRowDetailsFrozen",
- typeof(bool),
- typeof(DataGrid),
- null);
- #endregion AreRowDetailsFrozen
-
- #region AutoGenerateColumns
- /// <summary>
- /// Gets or sets a value indicating whether columns are created automatically when the ItemsSource
- /// property is set.
- /// </summary>
- public bool AutoGenerateColumns
- {
- get { return (bool)GetValue(AutoGenerateColumnsProperty); }
- set { SetValue(AutoGenerateColumnsProperty, value); }
- }
-
- /// <summary>
- /// Identifies the AutoGenerateColumns dependency property.
- /// </summary>
- public static readonly DependencyProperty AutoGenerateColumnsProperty =
- DependencyProperty.Register(
- "AutoGenerateColumns",
- typeof(bool),
- typeof(DataGrid),
- new PropertyMetadata(OnAutoGenerateColumnsPropertyChanged));
-
- private static void OnAutoGenerateColumnsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- bool value = (bool)e.NewValue;
- if (value)
- {
- dataGrid.RefreshRowsAndColumns();
- }
- else
- {
- dataGrid.RemoveAutoGeneratedColumns();
- }
- }
- #endregion AutoGenerateColumns
-
- #region CanUserReorderColumns
- /// <summary>
- /// Gets or sets a value indicating whether users can reorder columns.
- /// </summary>
- public bool CanUserReorderColumns
- {
- get { return (bool)GetValue(CanUserReorderColumnsProperty); }
- set { SetValue(CanUserReorderColumnsProperty, value); }
- }
-
- /// <summary>
- /// Identifies the CanUserReorderColumns dependency property.
- /// </summary>
- public static readonly DependencyProperty CanUserReorderColumnsProperty =
- DependencyProperty.Register(
- "CanUserReorderColumns",
- typeof(bool),
- typeof(DataGrid),
- null);
- #endregion CanUserReorderColumns
-
- #region CanUserResizeColumns
- /// <summary>
- /// Gets or sets a value indicating whether users can resize columns.
- /// </summary>
- public bool CanUserResizeColumns
- {
- get { return (bool)GetValue(CanUserResizeColumnsProperty); }
- set { SetValue(CanUserResizeColumnsProperty, value); }
- }
-
- /// <summary>
- /// Identifies the CanUserResizeColumns dependency property.
- /// </summary>
- public static readonly DependencyProperty CanUserResizeColumnsProperty =
- DependencyProperty.Register(
- "CanUserResizeColumns",
- typeof(bool),
- typeof(DataGrid),
- null);
- #endregion CanUserResizeColumns
-
- #region CanUserSortColumns
- /// <summary>
- /// Gets or sets a value indicating whether users can sort columns.
- /// </summary>
- public bool CanUserSortColumns
- {
- get { return (bool)GetValue(CanUserSortColumnsProperty); }
- set { SetValue(CanUserSortColumnsProperty, value); }
- }
-
- /// <summary>
- /// Identifies the CanUserSortColumns dependency property.
- /// </summary>
- public static readonly DependencyProperty CanUserSortColumnsProperty =
- DependencyProperty.Register(
- "CanUserSortColumns",
- typeof(bool),
- typeof(DataGrid),
- null);
- #endregion CanUserSortColumns
-
- #region CellStyle
- /// <summary>
- /// Gets or sets the style used by cells when they are rendered.
- /// </summary>
- public Style CellStyle
- {
- get { return GetValue(CellStyleProperty) as Style; }
- set { SetValue(CellStyleProperty, value); }
- }
-
- public static readonly DependencyProperty CellStyleProperty =
- DependencyProperty.Register(
- "CellStyle",
- typeof(Style),
- typeof(DataGrid),
- new PropertyMetadata(OnCellStylePropertyChanged));
-
- private static void OnCellStylePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- Style newStyle = e.NewValue as Style;
- if (newStyle != null)
- {
- DataGrid dataGrid = d as DataGrid;
- if (dataGrid != null && dataGrid._rowsPresenter != null)
- {
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- foreach (DataGridCell cell in row.Cells)
- {
- cell.EnsureCellStyle();
- }
- }
- dataGrid.InvalidateRowHeightEstimate();
- }
- }
- }
- #endregion CellStyle
-
- #region ColumnHeaderHeight
- /// <summary>
- /// Gets or sets the suggested height of the grid's column headers.
- /// </summary>
- public double ColumnHeaderHeight
- {
- get { return (double)GetValue(ColumnHeaderHeightProperty); }
- set { SetValue(ColumnHeaderHeightProperty, value); }
- }
-
- /// <summary>
- /// Identifies the ColumnHeaderHeight dependency property.
- /// </summary>
- public static readonly DependencyProperty ColumnHeaderHeightProperty =
- DependencyProperty.Register(
- "ColumnHeaderHeight",
- typeof(double),
- typeof(DataGrid),
- new PropertyMetadata(OnColumnHeaderHeightPropertyChanged));
-
- /// <summary>
- /// ColumnHeaderHeightProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its ColumnHeaderHeight.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnColumnHeaderHeightPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (!dataGrid.IsHandlerSuspended(e.Property))
- {
- double value = (double)e.NewValue;
- if (value < DATAGRID_minimumColumnHeaderHeight)
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueMustBeGreaterThanOrEqualTo("value", "ColumnHeaderHeight", DATAGRID_minimumColumnHeaderHeight);
- }
- if (value > DATAGRID_maxHeadersThickness)
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueMustBeLessThanOrEqualTo("value", "ColumnHeaderHeight", DATAGRID_maxHeadersThickness);
- }
- dataGrid.InvalidateMeasure();
- }
- }
- #endregion ColumnHeaderHeight
-
- #region ColumnHeaderStyle
- /// <summary>
- /// Gets or sets the style used by column headers when they are rendered.
- /// </summary>
- public Style ColumnHeaderStyle
- {
- get { return GetValue(ColumnHeaderStyleProperty) as Style; }
- set { SetValue(ColumnHeaderStyleProperty, value); }
- }
-
- /// <summary>
- /// Identifies the ColumnHeaderStyle dependency property.
- /// </summary>
- public static readonly DependencyProperty ColumnHeaderStyleProperty = DependencyProperty.Register("ColumnHeaderStyle", typeof(Style), typeof(DataGrid), new PropertyMetadata(OnColumnHeaderStylePropertyChanged));
-
- private static void OnColumnHeaderStylePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- //
- Style newStyle = e.NewValue as Style;
- if (newStyle != null)
- {
- DataGrid dataGrid = d as DataGrid;
- if (dataGrid != null)
- {
- Style oldStyle = e.OldValue as Style;
- foreach (DataGridColumn column in dataGrid.Columns)
- {
- EnsureColumnHeaderCellStyle(column, oldStyle, newStyle);
- }
- EnsureColumnHeaderCellStyle(dataGrid.ColumnsInternal.FillerColumn, oldStyle, newStyle);
- }
- }
- }
- #endregion ColumnHeaderStyle
-
- #region ColumnWidth
- /// <summary>
- /// Gets or sets the width of the grid's columns.
- /// </summary>
- public DataGridLength ColumnWidth
- {
- get { return (DataGridLength)GetValue(ColumnWidthProperty); }
- set { SetValue(ColumnWidthProperty, value); }
- }
-
- /// <summary>
- /// Identifies the ColumnWidth dependency property.
- /// </summary>
- public static readonly DependencyProperty ColumnWidthProperty =
- DependencyProperty.Register(
- "ColumnWidth",
- typeof(DataGridLength),
- typeof(DataGrid),
- new PropertyMetadata(OnColumnWidthPropertyChanged));
-
- /// <summary>
- /// ColumnWidthProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its ColumnWidth.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnColumnWidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
-
- dataGrid.EnsureHorizontalLayout();
- }
- #endregion ColumnWidth
-
- #region DragIndicatorStyle
- /// <summary>
- /// Gets or sets the style used by rows when they are rendered.
- /// </summary>
- public Style DragIndicatorStyle
- {
- get { return GetValue(DragIndicatorStyleProperty) as Style; }
- set { SetValue(DragIndicatorStyleProperty, value); }
- }
-
- public static readonly DependencyProperty DragIndicatorStyleProperty =
- DependencyProperty.Register(
- "DragIndicatorStyle",
- typeof(Style),
- typeof(DataGrid),
- null);
- #endregion DragIndicatorStyle
-
- #region DropLocationIndicatorStyle
- /// <summary>
- /// Gets or sets the style used by rows when they are rendered.
- /// </summary>
- public Style DropLocationIndicatorStyle
- {
- get { return GetValue(DropLocationIndicatorStyleProperty) as Style; }
- set { SetValue(DropLocationIndicatorStyleProperty, value); }
- }
-
- public static readonly DependencyProperty DropLocationIndicatorStyleProperty =
- DependencyProperty.Register(
- "DropLocationIndicatorStyle",
- typeof(Style),
- typeof(DataGrid),
- null);
- #endregion DropLocationIndicatorStyle
-
- #region FrozenColumnCount
-
- public int FrozenColumnCount
- {
- get { return (int)GetValue(FrozenColumnCountProperty); }
- set { SetValue(FrozenColumnCountProperty, value); }
- }
-
- public static readonly DependencyProperty FrozenColumnCountProperty =
- DependencyProperty.Register(
- "FrozenColumnCount",
- typeof(int),
- typeof(DataGrid),
- new PropertyMetadata(OnFrozenColumnCountPropertyChanged));
-
- private static void OnFrozenColumnCountPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- ProcessFrozenColumnCount((DataGrid)d);
- }
-
- private static void ProcessFrozenColumnCount(DataGrid dataGrid)
- {
- dataGrid.CorrectColumnFrozenStates();
- dataGrid.ComputeScrollBarsLayout();
-
- dataGrid.InvalidateColumnHeadersArrange();
- dataGrid.InvalidateCellsArrange();
- }
-
- #endregion FrozenColumnCount
-
- #region GridLinesVisibility
- /// <summary>
- /// Gets or sets a value that indicates whether horizontal or vertical gridlines for
- /// the inner cells should be displayed.
- /// </summary>
- public DataGridGridLinesVisibility GridLinesVisibility
- {
- get { return (DataGridGridLinesVisibility)GetValue(GridLinesVisibilityProperty); }
- set { SetValue(GridLinesVisibilityProperty, value); }
- }
-
- /// <summary>
- /// Identifies the GridLines dependency property.
- /// </summary>
- public static readonly DependencyProperty GridLinesVisibilityProperty =
- DependencyProperty.Register(
- "GridLinesVisibility",
- typeof(DataGridGridLinesVisibility),
- typeof(DataGrid),
- new PropertyMetadata(OnGridLinesVisibilityPropertyChanged));
-
- /// <summary>
- /// GridLinesProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its GridLines.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnGridLinesVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (!dataGrid.IsHandlerSuspended(e.Property) && dataGrid._rowsPresenter != null)
- {
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- row.EnsureGridLines();
- row.InvalidateHorizontalArrange();
- }
- }
- }
- #endregion GridLinesVisibility
-
- #region HeadersVisibility
- /// <summary>
- /// Gets or sets a value that indicates whether column or row headers should be displayed.
- /// </summary>
- public DataGridHeadersVisibility HeadersVisibility
- {
- get { return (DataGridHeadersVisibility)GetValue(HeadersVisibilityProperty); }
- set { SetValue(HeadersVisibilityProperty, value); }
- }
-
- /// <summary>
- /// Identifies the HeadersVisibility dependency property.
- /// </summary>
- public static readonly DependencyProperty HeadersVisibilityProperty =
- DependencyProperty.Register(
- "HeadersVisibility",
- typeof(DataGridHeadersVisibility),
- typeof(DataGrid),
- new PropertyMetadata(OnHeadersVisibilityPropertyChanged));
-
- /// <summary>
- /// HeadersVisibilityProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its HeadersVisibility.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnHeadersVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (!dataGrid.IsHandlerSuspended(e.Property))
- {
- DataGridHeadersVisibility newValue = (DataGridHeadersVisibility)e.NewValue;
- DataGridHeadersVisibility oldValue = (DataGridHeadersVisibility)e.OldValue;
-
- Func<DataGridHeadersVisibility, DataGridHeadersVisibility, bool> hasFlags = (DataGridHeadersVisibility value, DataGridHeadersVisibility flags) => ((value & flags) == flags);
-
- bool newValueCols = hasFlags(newValue, DataGridHeadersVisibility.Column);
- bool newValueRows = hasFlags(newValue, DataGridHeadersVisibility.Row);
- bool oldValueCols = hasFlags(oldValue, DataGridHeadersVisibility.Column);
- bool oldValueRows = hasFlags(oldValue, DataGridHeadersVisibility.Row);
-
- // Columns
- if (newValueCols != oldValueCols)
- {
- if (dataGrid._columnHeadersPresenter != null)
- {
- dataGrid.EnsureColumnHeadersVisibility();
- if (!newValueCols)
- {
- dataGrid._columnHeadersPresenter.Measure(Size.Empty);
- }
- dataGrid.InvalidateMeasure();
- }
- }
-
- // Rows
- if (newValueRows != oldValueRows)
- {
- if (dataGrid._rowsPresenter != null)
- {
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- row.EnsureHeaderStyleAndVisibility();
- if (newValueRows)
- {
- row.ApplyState(false /*animate*/);
- row.EnsureHeaderVisibility();
- }
- }
- dataGrid.InvalidateRowHeightEstimate();
- dataGrid.InvalidateRowsMeasure(true /*invalidateIndividualRows*/);
- }
- }
-
- //
-
- if (dataGrid._topLeftCornerHeader != null)
- {
- dataGrid._topLeftCornerHeader.Visibility = newValueRows && newValueCols ? Visibility.Visible : Visibility.Collapsed;
- if (dataGrid._topLeftCornerHeader.Visibility == Visibility.Collapsed)
- {
- dataGrid._topLeftCornerHeader.Measure(Size.Empty);
- }
- }
- }
- }
- #endregion HeadersVisibility
-
- #region HorizontalGridLinesBrush
- /// <summary>
- /// Gets or sets a brush that describes the horizontal gridlines color.
- /// </summary>
- public Brush HorizontalGridLinesBrush
- {
- get { return GetValue(HorizontalGridLinesBrushProperty) as Brush; }
- set { SetValue(HorizontalGridLinesBrushProperty, value); }
- }
-
- /// <summary>
- /// Identifies the HorizontalGridLinesBrush dependency property.
- /// </summary>
- public static readonly DependencyProperty HorizontalGridLinesBrushProperty =
- DependencyProperty.Register(
- "HorizontalGridLinesBrush",
- typeof(Brush),
- typeof(DataGrid),
- new PropertyMetadata(OnHorizontalGridLinesBrushPropertyChanged));
-
- /// <summary>
- /// HorizontalGridLinesBrushProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its HorizontalGridLinesBrush.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnHorizontalGridLinesBrushPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (!dataGrid.IsHandlerSuspended(e.Property) && dataGrid._rowsPresenter != null)
- {
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- row.EnsureGridLines();
- }
- }
- }
- #endregion HorizontalGridLinesBrush
-
- #region HorizontalScrollBarVisibility
- /// <summary>
- /// Gets or sets a value that indicates whether a horizontal ScrollBar should be displayed.
- /// </summary>
- public ScrollBarVisibility HorizontalScrollBarVisibility
- {
- get { return (ScrollBarVisibility)GetValue(HorizontalScrollBarVisibilityProperty); }
- set { SetValue(HorizontalScrollBarVisibilityProperty, value); }
- }
-
- /// <summary>
- /// Identifies the HorizontalScrollBarVisibility dependency property.
- /// </summary>
- public static readonly DependencyProperty HorizontalScrollBarVisibilityProperty =
- DependencyProperty.Register(
- "HorizontalScrollBarVisibility",
- typeof(ScrollBarVisibility),
- typeof(DataGrid),
- new PropertyMetadata(OnHorizontalScrollBarVisibilityPropertyChanged));
-
- /// <summary>
- /// HorizontalScrollBarVisibilityProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its HorizontalScrollBarVisibility.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnHorizontalScrollBarVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (!dataGrid.IsHandlerSuspended(e.Property) &&
- (ScrollBarVisibility)e.NewValue != (ScrollBarVisibility)e.OldValue &&
- dataGrid._hScrollBar != null)
- {
- dataGrid.InvalidateMeasure();
- }
- }
- #endregion HorizontalScrollBarVisibility
-
- #region IsReadOnly
- /// <summary>
- /// Gets or sets a value indicating whether the user can edit the cells of the DataGrid control.
- /// </summary>
- public bool IsReadOnly
- {
- get { return (bool)GetValue(IsReadOnlyProperty); }
- set { SetValue(IsReadOnlyProperty, value); }
- }
-
- /// <summary>
- /// Identifies the IsReadOnly dependency property.
- /// </summary>
- public static readonly DependencyProperty IsReadOnlyProperty =
- DependencyProperty.Register(
- "IsReadOnly",
- typeof(bool),
- typeof(DataGrid),
- new PropertyMetadata(OnIsReadOnlyPropertyChanged));
-
- /// <summary>
- /// IsReadOnlyProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its IsReadOnly.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnIsReadOnlyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (!dataGrid.IsHandlerSuspended(e.Property))
- {
- bool value = (bool)e.NewValue;
- if (value && !dataGrid.CommitEdit(DataGridEditingUnit.Row, true /*exitEditing*/))
- {
- d.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.CommitFailedCannotCompleteOperation();
- }
- }
- }
- #endregion IsReadOnly
-
- #region ItemsSource
- /// <summary>
- /// Gets or sets a collection used to generate the content of the DataGrid.
- /// </summary>
- public IEnumerable ItemsSource
- {
- get { return GetValue(ItemsSourceProperty) as IEnumerable; }
- set { SetValue(ItemsSourceProperty, value); }
- }
-
- /// <summary>
- /// Identifies the ItemsSource dependency property.
- /// </summary>
- public static readonly DependencyProperty ItemsSourceProperty =
- DependencyProperty.Register(
- "ItemsSource",
- typeof(IEnumerable),
- typeof(DataGrid),
- new PropertyMetadata(OnItemsSourcePropertyChanged));
-
- /// <summary>
- /// ItemsSourceProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its ItemsSource.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (!dataGrid.IsHandlerSuspended(e.Property))
- {
- Debug.Assert(dataGrid.DataConnection != null);
-
- if (dataGrid.LoadingOrUnloadingRow)
- {
- dataGrid.SetValueNoCallback(ItemsSourceProperty, e.OldValue);
- throw DataGridError.DataGrid.CannotChangeItemsWhenLoadingRows();
- }
-
- dataGrid.DataConnection.UnWireEvents(dataGrid.DataConnection.DataSource);
- dataGrid.DataConnection.ClearDataProperties();
-
- // Wrap an IList in a CollectionView if it's not already one
- IEnumerable newDataSource;
- IList tempList = e.NewValue as IList;
- if (tempList != null && !(e.NewValue is System.ComponentModel.ICollectionView))
- {
- newDataSource = new ListCollectionView(tempList);
- }
- else
- {
- newDataSource = (IEnumerable)e.NewValue;
- }
- dataGrid.DataConnection.DataSource = newDataSource;
-
- if (newDataSource != null)
- {
- dataGrid.DataConnection.WireEvents(newDataSource);
- }
-
- // we always want to do this
- dataGrid.RefreshRowsAndColumns();
- }
- }
-
- #endregion ItemsSource
-
- #region MaxColumnWidth
- /// <summary>
- /// Gets or sets the width of the grid's columns.
- /// </summary>
- public double MaxColumnWidth
- {
- get { return (double)GetValue(MaxColumnWidthProperty); }
- set { SetValue(MaxColumnWidthProperty, value); }
- }
-
- /// <summary>
- /// Identifies the MaxColumnWidth dependency property.
- /// </summary>
- public static readonly DependencyProperty MaxColumnWidthProperty =
- DependencyProperty.Register(
- "MaxColumnWidth",
- typeof(double),
- typeof(DataGrid),
- new PropertyMetadata(OnMaxColumnWidthPropertyChanged));
-
- /// <summary>
- /// MaxColumnWidthProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its ColumnWidth.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnMaxColumnWidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- double newValue = (double)e.NewValue;
-
- if (double.IsNaN(newValue))
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueCannotBeSetToNAN("MaxColumnWidth");
- }
- if (newValue < 0)
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueMustBeGreaterThanOrEqualTo("value", "MaxColumnWidth", 0);
- }
- if (dataGrid.MinColumnWidth > newValue)
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueMustBeGreaterThanOrEqualTo("value", "MaxColumnWidth", "MinColumnWidth");
- }
-
- dataGrid.ColumnsInternal.EnsureVisibleEdgedColumnsWidth();
- dataGrid.InvalidateColumnHeadersMeasure();
- dataGrid.InvalidateRowsMeasure(true);
- }
- #endregion MaxColumnWidth
-
- #region MinColumnWidth
- /// <summary>
- /// Gets or sets the width of the grid's columns.
- /// </summary>
- public double MinColumnWidth
- {
- get { return (double)GetValue(MinColumnWidthProperty); }
- set { SetValue(MinColumnWidthProperty, value); }
- }
-
- /// <summary>
- /// Identifies the MinColumnWidth dependency property.
- /// </summary>
- public static readonly DependencyProperty MinColumnWidthProperty =
- DependencyProperty.Register(
- "MinColumnWidth",
- typeof(double),
- typeof(DataGrid),
- new PropertyMetadata(OnMinColumnWidthPropertyChanged));
-
- /// <summary>
- /// MinColumnWidthProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its ColumnWidth.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnMinColumnWidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- double newValue = (double)e.NewValue;
-
- if (double.IsNaN(newValue))
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueCannotBeSetToNAN("MinColumnWidth");
- }
- if (newValue < 0)
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueMustBeGreaterThanOrEqualTo("value", "MinColumnWidth", 0);
- }
- if (double.IsPositiveInfinity(newValue))
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueCannotBeSetToInfinity("MinColumnWidth");
- }
- if (dataGrid.MaxColumnWidth < newValue)
- {
- dataGrid.SetValueNoCallback(e.Property, e.OldValue);
- throw DataGridError.DataGrid.ValueMustBeLessThanOrEqualTo("value", "MinColumnWidth", "MaxColumnWidth");
- }
-
- dataGrid.ColumnsInternal.EnsureVisibleEdgedColumnsWidth();
- dataGrid.InvalidateColumnHeadersMeasure();
- dataGrid.InvalidateRowsMeasure(true);
- }
- #endregion MinColumnWidth
-
- #region RowBackground
- /// <summary>
- /// Gets or sets a brush that describes the background of a row in the grid.
- /// </summary>
- public Brush RowBackground
- {
- get { return GetValue(RowBackgroundProperty) as Brush; }
- set { SetValue(RowBackgroundProperty, value); }
- }
-
- public static readonly DependencyProperty RowBackgroundProperty = DependencyProperty.Register("RowBackground", typeof(Brush), typeof(DataGrid), new PropertyMetadata(OnRowBackgroundPropertyChanged));
-
- private static void OnRowBackgroundPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
-
- if (dataGrid._rowsPresenter != null)
- {
- // Go through the Displayed rows and update the background
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- row.EnsureBackground();
- }
- }
- }
- #endregion RowBackground
-
- #region RowDetailsTemplate
- /// <summary>
- /// Gets or sets the DataTemplate used to display the details section of a row.
- /// </summary>
- public DataTemplate RowDetailsTemplate
- {
- get { return GetValue(RowDetailsTemplateProperty) as DataTemplate; }
- set { SetValue(RowDetailsTemplateProperty, value); }
- }
-
- /// <summary>
- /// Identifies the RowDetailsTemplate dependency property.
- /// </summary>
- public static readonly DependencyProperty RowDetailsTemplateProperty =
- DependencyProperty.Register(
- "RowDetailsTemplate",
- typeof(DataTemplate),
- typeof(DataGrid),
- new PropertyMetadata(OnRowDetailsTemplatePropertyChanged));
-
- private static void OnRowDetailsTemplatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
-
- // Update the RowDetails templates if necessary
- if (dataGrid._rowsPresenter != null)
- {
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- if (dataGrid.GetRowDetailsVisibility(row.Index) == Visibility.Visible)
- {
- // DetailsPreferredHeight is initialized when the DetailsElement's size changes.
- row.ApplyDetailsTemplate(false /*initializeDetailsPreferredHeight*/);
- }
- }
- }
-
- dataGrid.UpdateRowDetailsHeightEstimate();
- dataGrid.InvalidateMeasure();
- }
-
- #endregion RowDetailsTemplate
-
- #region RowDetailsVisibilityMode
- /// <summary>
- /// Gets or sets a value that indicates when the details section of a row should be displayed.
- /// </summary>
- //
- [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
- public DataGridRowDetailsVisibilityMode RowDetailsVisibilityMode
- {
- get { return (DataGridRowDetailsVisibilityMode)GetValue(RowDetailsVisibilityModeProperty); }
- set { SetValue(RowDetailsVisibilityModeProperty, value); }
- }
-
- /// <summary>
- /// Identifies the RowDetailsVisibilityMode dependency property.
- /// </summary>
- public static readonly DependencyProperty RowDetailsVisibilityModeProperty =
- DependencyProperty.Register(
- "RowDetailsVisibilityMode",
- typeof(DataGridRowDetailsVisibilityMode),
- typeof(DataGrid),
- new PropertyMetadata(OnRowDetailsVisibilityModePropertyChanged));
-
- /// <summary>
- /// RowDetailsVisibilityModeProperty property changed handler.
- /// </summary>
- /// <param name="d">DataGrid that changed its RowDetailsVisibilityMode.</param>
- /// <param name="e">DependencyPropertyChangedEventArgs.</param>
- private static void OnRowDetailsVisibilityModePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- DataGrid dataGrid = (DataGrid)d;
- if (dataGrid._rowsPresenter != null && dataGrid.RowCount > 0)
- {
- DataGridRowDetailsVisibilityMode newDetailsMode = (DataGridRowDetailsVisibilityMode)e.NewValue;
- Visibility newDetailsVisibility = Visibility.Collapsed;
- switch (newDetailsMode)
- {
- case DataGridRowDetailsVisibilityMode.Visible:
- newDetailsVisibility = Visibility.Visible;
- dataGrid._showDetailsTable.AddValues(0, dataGrid.RowCount, Visibility.Visible);
- break;
- case DataGridRowDetailsVisibilityMode.Collapsed:
- newDetailsVisibility = Visibility.Collapsed;
- dataGrid._showDetailsTable.AddValues(0, dataGrid.RowCount, Visibility.Collapsed);
- break;
- case DataGridRowDetailsVisibilityMode.VisibleWhenSelected:
- dataGrid._showDetailsTable.Clear();
- break;
- }
-
- bool updated = false;
- foreach (DataGridRow row in dataGrid._rowsPresenter.Children)
- {
- Debug.Assert(row.Index != -1);
- if (newDetailsMode == DataGridRowDetailsVisibilityMode.VisibleWhenSelected)
- {
- // For VisibleWhenSelected, we need to calculate the value for each individual row
- newDetailsVisibility = dataGrid._selectedItems.Contains(row.Index) ? Visibility.Visible : Visibility.Collapsed;
- }
- if (row.DetailsVisibility != newDetailsVisibility)
- {
- updated = true;
- row.SetDetailsVisibilityInternal(newDetailsVisibility, true /* raiseNotification */, false /* animate */);
- }
- }
- if (updated)
- {
- dataGrid.UpdateDisplayedRows(dataGrid.DisplayData.FirstDisplayedScrollingRow, dataGrid.CellsHeight);
- dataGrid.InvalidateRowsMeasure(false /*invalidateIndividualRows*/);
- …
Large files files are truncated, but you can click here to view the full file