| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- // (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 Avalonia.Controls.Primitives;
- using Avalonia.Controls.Shapes;
- using Avalonia.Input;
- namespace Avalonia.Controls
- {
- /// <summary>
- /// Represents an individual <see cref="T:Avalonia.Controls.DataGrid" /> cell.
- /// </summary>
- public class DataGridCell : ContentControl
- {
- private const string DATAGRIDCELL_elementRightGridLine = "PART_RightGridLine";
- private Rectangle _rightGridLine;
- private DataGridColumn _owningColumn;
- bool _isValid;
- public static readonly DirectProperty<DataGridCell, bool> IsValidProperty =
- AvaloniaProperty.RegisterDirect<DataGridCell, bool>(
- nameof(IsValid),
- o => o.IsValid);
- static DataGridCell()
- {
- PointerPressedEvent.AddClassHandler<DataGridCell>(
- (x,e) => x.DataGridCell_PointerPressed(e), handledEventsToo: true);
- }
- public DataGridCell()
- { }
- public bool IsValid
- {
- get { return _isValid; }
- internal set { SetAndRaise(IsValidProperty, ref _isValid, value); }
- }
- internal DataGridColumn OwningColumn
- {
- get => _owningColumn;
- set
- {
- if (_owningColumn != value)
- {
- _owningColumn = value;
- OnOwningColumnSet(value);
- }
- }
- }
- internal DataGridRow OwningRow
- {
- get;
- set;
- }
- internal DataGrid OwningGrid
- {
- get { return OwningRow?.OwningGrid ?? OwningColumn?.OwningGrid; }
- }
- internal double ActualRightGridLineWidth
- {
- get { return _rightGridLine?.Bounds.Width ?? 0; }
- }
- internal int ColumnIndex
- {
- get { return OwningColumn?.Index ?? -1; }
- }
- internal int RowIndex
- {
- get { return OwningRow?.Index ?? -1; }
- }
- internal bool IsCurrent
- {
- get
- {
- return OwningGrid.CurrentColumnIndex == OwningColumn.Index &&
- OwningGrid.CurrentSlot == OwningRow.Slot;
- }
- }
- private bool IsEdited
- {
- get
- {
- return OwningGrid.EditingRow == OwningRow &&
- OwningGrid.EditingColumnIndex == ColumnIndex;
- }
- }
- private bool IsMouseOver
- {
- get
- {
- return OwningRow != null && OwningRow.MouseOverColumnIndex == ColumnIndex;
- }
- set
- {
- if (value != IsMouseOver)
- {
- if (value)
- {
- OwningRow.MouseOverColumnIndex = ColumnIndex;
- }
- else
- {
- OwningRow.MouseOverColumnIndex = null;
- }
- }
- }
- }
- /// <summary>
- /// Builds the visual tree for the cell control when a new template is applied.
- /// </summary>
- protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
- {
- base.OnTemplateApplied(e);
- UpdatePseudoClasses();
- _rightGridLine = e.NameScope.Find<Rectangle>(DATAGRIDCELL_elementRightGridLine);
- if (_rightGridLine != null && OwningColumn == null)
- {
- // Turn off the right GridLine for filler cells
- _rightGridLine.IsVisible = false;
- }
- else
- {
- EnsureGridLine(null);
- }
- }
- protected override void OnPointerEnter(PointerEventArgs e)
- {
- base.OnPointerEnter(e);
- if (OwningRow != null)
- {
- IsMouseOver = true;
- }
- }
- protected override void OnPointerLeave(PointerEventArgs e)
- {
- base.OnPointerLeave(e);
- if (OwningRow != null)
- {
- IsMouseOver = false;
- }
- }
- //TODO TabStop
- private void DataGridCell_PointerPressed(PointerPressedEventArgs e)
- {
- // OwningGrid is null for TopLeftHeaderCell and TopRightHeaderCell because they have no OwningRow
- if (OwningGrid != null)
- {
- OwningGrid.OnCellPointerPressed(new DataGridCellPointerPressedEventArgs(this, OwningRow, OwningColumn, e));
- if (e.MouseButton == MouseButton.Left)
- {
- if (!e.Handled)
- //if (!e.Handled && OwningGrid.IsTabStop)
- {
- OwningGrid.Focus();
- }
- if (OwningRow != null)
- {
- e.Handled = OwningGrid.UpdateStateOnMouseLeftButtonDown(e, ColumnIndex, OwningRow.Slot, !e.Handled);
- OwningGrid.UpdatedStateOnMouseLeftButtonDown = true;
- }
- }
- }
- }
- internal void UpdatePseudoClasses()
- {
- }
- // Makes sure the right gridline has the proper stroke and visibility. If lastVisibleColumn is specified, the
- // right gridline will be collapsed if this cell belongs to the lastVisibileColumn and there is no filler column
- internal void EnsureGridLine(DataGridColumn lastVisibleColumn)
- {
- if (OwningGrid != null && _rightGridLine != null)
- {
- if (OwningGrid.VerticalGridLinesBrush != null && OwningGrid.VerticalGridLinesBrush != _rightGridLine.Fill)
- {
- _rightGridLine.Fill = OwningGrid.VerticalGridLinesBrush;
- }
- bool newVisibility =
- (OwningGrid.GridLinesVisibility == DataGridGridLinesVisibility.Vertical || OwningGrid.GridLinesVisibility == DataGridGridLinesVisibility.All)
- && (OwningGrid.ColumnsInternal.FillerColumn.IsActive || OwningColumn != lastVisibleColumn);
- if (newVisibility != _rightGridLine.IsVisible)
- {
- _rightGridLine.IsVisible = newVisibility;
- }
- }
- }
- private void OnOwningColumnSet(DataGridColumn column)
- {
- if (column == null)
- {
- Classes.Clear();
- }
- else
- {
- Classes.Replace(column.CellStyleClasses);
- }
- }
- }
- }
|