mirror of
https://github.com/AtomUI/AtomUI.git
synced 2026-10-28 23:08:36 +08:00
Review DataGrid.Rows
This commit is contained in:
@@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using AtomUI.Controls.Collections;
|
||||
using AtomUI.Utils;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Layout;
|
||||
@@ -154,7 +155,7 @@ public partial class DataGrid
|
||||
}
|
||||
else
|
||||
{
|
||||
return DataConnection!.GetPropertyIsReadOnly(path) || isReadOnly;
|
||||
return DataConnection.GetPropertyIsReadOnly(path) || isReadOnly;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +204,7 @@ public partial class DataGrid
|
||||
RemoveDisplayedColumnHeaders();
|
||||
|
||||
_horizontalOffset = _negHorizontalOffset = 0;
|
||||
if (_hScrollBar != null && _hScrollBar.IsVisible) //
|
||||
if (_hScrollBar != null && _hScrollBar.IsVisible)
|
||||
{
|
||||
_hScrollBar.Value = 0;
|
||||
}
|
||||
@@ -253,7 +254,7 @@ public partial class DataGrid
|
||||
internal void OnColumnDisplayIndexChanged(DataGridColumn dataGridColumn)
|
||||
{
|
||||
Debug.Assert(dataGridColumn != null);
|
||||
DataGridColumnEventArgs e = new DataGridColumnEventArgs(dataGridColumn);
|
||||
var e = new DataGridColumnEventArgs(dataGridColumn);
|
||||
|
||||
// Call protected method to raise event
|
||||
if (dataGridColumn != ColumnsInternal.RowGroupSpacerColumn)
|
||||
@@ -290,7 +291,7 @@ public partial class DataGrid
|
||||
{
|
||||
InDisplayIndexAdjustments = true;
|
||||
|
||||
bool trackChange = targetColumn != ColumnsInternal.RowGroupSpacerColumn;
|
||||
var trackChange = targetColumn != ColumnsInternal.RowGroupSpacerColumn;
|
||||
|
||||
DataGridColumn? column;
|
||||
// Move is legal - let's adjust the affected display indexes.
|
||||
@@ -360,9 +361,9 @@ public partial class DataGrid
|
||||
{
|
||||
Debug.Assert(column != null);
|
||||
|
||||
if (column.IsVisible && oldValue != column.ActualMaxWidth)
|
||||
if (column.IsVisible && !MathUtils.AreClose(oldValue, column.ActualMaxWidth))
|
||||
{
|
||||
DataGridLength oldWitdh = new(oldValue, column.Width.UnitType, column.Width.DesiredValue,
|
||||
DataGridLength oldWidth = new(oldValue, column.Width.UnitType, column.Width.DesiredValue,
|
||||
column.Width.DesiredValue);
|
||||
if (column.ActualMaxWidth < column.Width.DisplayValue)
|
||||
{
|
||||
@@ -372,11 +373,11 @@ public partial class DataGrid
|
||||
AdjustColumnWidths(column.DisplayIndex + 1, column.Width.DisplayValue - column.ActualMaxWidth, false);
|
||||
column.SetWidthDisplayValue(column.ActualMaxWidth);
|
||||
}
|
||||
else if (column.Width.DisplayValue == oldValue && column.Width.DesiredValue > column.Width.DisplayValue)
|
||||
else if (MathUtils.AreClose(column.Width.DisplayValue, oldValue) && column.Width.DesiredValue > column.Width.DisplayValue)
|
||||
{
|
||||
// If the column was previously limited by its maximum value but has more room now,
|
||||
// attempt to resize the column to its desired width.
|
||||
column.Resize(oldWitdh,
|
||||
column.Resize(oldWidth,
|
||||
new(column.Width.Value, column.Width.UnitType, column.Width.DesiredValue,
|
||||
column.Width.DesiredValue),
|
||||
false);
|
||||
@@ -395,9 +396,9 @@ public partial class DataGrid
|
||||
{
|
||||
Debug.Assert(column != null);
|
||||
|
||||
if (column.IsVisible && oldValue != column.ActualMinWidth)
|
||||
if (column.IsVisible && !MathUtils.AreClose(oldValue, column.ActualMinWidth))
|
||||
{
|
||||
DataGridLength oldWitdh = new(oldValue, column.Width.UnitType, column.Width.DesiredValue,
|
||||
DataGridLength oldWidth = new(oldValue, column.Width.UnitType, column.Width.DesiredValue,
|
||||
column.Width.DesiredValue);
|
||||
if (column.ActualMinWidth > column.Width.DisplayValue)
|
||||
{
|
||||
@@ -407,11 +408,11 @@ public partial class DataGrid
|
||||
AdjustColumnWidths(column.DisplayIndex + 1, column.Width.DisplayValue - column.ActualMinWidth, false);
|
||||
column.SetWidthDisplayValue(column.ActualMinWidth);
|
||||
}
|
||||
else if (column.Width.DisplayValue == oldValue && column.Width.DesiredValue < column.Width.DisplayValue)
|
||||
else if (MathUtils.AreClose(column.Width.DisplayValue, oldValue) && column.Width.DesiredValue < column.Width.DisplayValue)
|
||||
{
|
||||
// If the column was previously limited by its minimum value but but can be smaller now,
|
||||
// attempt to resize the column to its desired width.
|
||||
column.Resize(oldWitdh,
|
||||
column.Resize(oldWidth,
|
||||
new(column.Width.Value, column.Width.UnitType, column.Width.DesiredValue,
|
||||
column.Width.DesiredValue),
|
||||
false);
|
||||
@@ -506,8 +507,8 @@ public partial class DataGrid
|
||||
|
||||
internal void OnFillerColumnWidthNeeded(double finalWidth)
|
||||
{
|
||||
DataGridFillerColumn fillerColumn = ColumnsInternal.FillerColumn;
|
||||
double totalColumnsWidth = ColumnsInternal.VisibleEdgedColumnsWidth;
|
||||
var fillerColumn = ColumnsInternal.FillerColumn;
|
||||
var totalColumnsWidth = ColumnsInternal.VisibleEdgedColumnsWidth;
|
||||
if (finalWidth - totalColumnsWidth > LayoutHelper.LayoutEpsilon)
|
||||
{
|
||||
fillerColumn.FillerWidth = finalWidth - totalColumnsWidth;
|
||||
@@ -552,7 +553,7 @@ public partial class DataGrid
|
||||
// Insert the missing data cells
|
||||
if (SlotCount > 0)
|
||||
{
|
||||
int newColumnCount = ColumnsItemsInternal.Count;
|
||||
var newColumnCount = ColumnsItemsInternal.Count;
|
||||
|
||||
foreach (DataGridRow row in GetAllRows())
|
||||
{
|
||||
@@ -658,16 +659,15 @@ public partial class DataGrid
|
||||
DataGridCellCoordinates newCurrentCellCoordinates;
|
||||
|
||||
_temporarilyResetCurrentCell = false;
|
||||
int columnIndex = dataGridColumn.Index;
|
||||
var columnIndex = dataGridColumn.Index;
|
||||
|
||||
// Reset the current cell's address if there is one.
|
||||
if (CurrentColumnIndex != -1)
|
||||
{
|
||||
int newCurrentColumnIndex = CurrentColumnIndex;
|
||||
var newCurrentColumnIndex = CurrentColumnIndex;
|
||||
if (columnIndex == newCurrentColumnIndex)
|
||||
{
|
||||
DataGridColumn? dataGridColumnNext =
|
||||
ColumnsInternal.GetNextVisibleColumn(ColumnsItemsInternal[columnIndex]);
|
||||
var dataGridColumnNext = ColumnsInternal.GetNextVisibleColumn(ColumnsItemsInternal[columnIndex]);
|
||||
if (dataGridColumnNext != null)
|
||||
{
|
||||
if (dataGridColumnNext.Index > columnIndex)
|
||||
@@ -681,8 +681,7 @@ public partial class DataGrid
|
||||
}
|
||||
else
|
||||
{
|
||||
DataGridColumn? dataGridColumnPrevious =
|
||||
ColumnsInternal.GetPreviousVisibleNonFillerColumn(ColumnsItemsInternal[columnIndex]);
|
||||
var dataGridColumnPrevious = ColumnsInternal.GetPreviousVisibleNonFillerColumn(ColumnsItemsInternal[columnIndex]);
|
||||
if (dataGridColumnPrevious != null)
|
||||
{
|
||||
if (dataGridColumnPrevious.Index > columnIndex)
|
||||
@@ -715,7 +714,7 @@ public partial class DataGrid
|
||||
CancelEdit(DataGridEditingUnit.Row, false /*raiseEvents*/);
|
||||
}
|
||||
|
||||
bool success = SetCurrentCellCore(-1, -1);
|
||||
var success = SetCurrentCellCore(-1, -1);
|
||||
Debug.Assert(success);
|
||||
}
|
||||
else
|
||||
@@ -775,7 +774,7 @@ public partial class DataGrid
|
||||
Debug.Assert(dataGridColumn != null);
|
||||
|
||||
// Take care of the non-displayed loaded rows
|
||||
for (int index = 0; index < _loadedRows.Count;)
|
||||
for (var index = 0; index < _loadedRows.Count;)
|
||||
{
|
||||
DataGridRow dataGridRow = _loadedRows[index];
|
||||
Debug.Assert(dataGridRow != null);
|
||||
@@ -812,20 +811,20 @@ public partial class DataGrid
|
||||
/// <returns>The remaining amount of adjustment.</returns>
|
||||
private double AdjustStarColumnWidths(int displayIndex, double adjustment, bool userInitiated)
|
||||
{
|
||||
double remainingAdjustment = adjustment;
|
||||
var remainingAdjustment = adjustment;
|
||||
if (MathUtilities.IsZero(remainingAdjustment))
|
||||
{
|
||||
return remainingAdjustment;
|
||||
}
|
||||
|
||||
bool increase = remainingAdjustment > 0;
|
||||
var increase = remainingAdjustment > 0;
|
||||
|
||||
// Make an initial pass through the star columns to total up some values.
|
||||
bool scaleStarWeights = false;
|
||||
double totalStarColumnsWidth = 0;
|
||||
double totalStarColumnsWidthLimit = 0;
|
||||
double totalStarWeights = 0;
|
||||
List<DataGridColumn> starColumns = new List<DataGridColumn>();
|
||||
var scaleStarWeights = false;
|
||||
var totalStarColumnsWidth = 0d;
|
||||
var totalStarColumnsWidthLimit = 0d;
|
||||
var totalStarWeights = 0d;
|
||||
var starColumns = new List<DataGridColumn>();
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetDisplayedColumns(c =>
|
||||
c.Width.IsStar && c.IsVisible && (c.ActualCanUserResize || !userInitiated)))
|
||||
{
|
||||
@@ -843,7 +842,7 @@ public partial class DataGrid
|
||||
|
||||
// Set the new desired widths according to how much all the star columns can be adjusted without any
|
||||
// of them being limited by their minimum or maximum widths (as that would distort their ratios).
|
||||
double adjustmentLimit = totalStarColumnsWidthLimit - totalStarColumnsWidth;
|
||||
var adjustmentLimit = totalStarColumnsWidthLimit - totalStarColumnsWidth;
|
||||
adjustmentLimit = increase ? Math.Min(adjustmentLimit, adjustment) : Math.Max(adjustmentLimit, adjustment);
|
||||
foreach (DataGridColumn starColumn in starColumns)
|
||||
{
|
||||
@@ -891,10 +890,10 @@ public partial class DataGrid
|
||||
return remainingAdjustment;
|
||||
}
|
||||
|
||||
bool increase = remainingAdjustment > 0;
|
||||
var increase = remainingAdjustment > 0;
|
||||
|
||||
double totalStarWeights = 0;
|
||||
double totalStarColumnsWidth = 0;
|
||||
var totalStarWeights = 0d;
|
||||
var totalStarColumnsWidth = 0d;
|
||||
|
||||
// Order the star columns according to which one will hit their target width (or min/max limit) first.
|
||||
// Each KeyValuePair represents a column (as the key) and an ordering factor (as the value). The ordering factor
|
||||
@@ -931,9 +930,9 @@ public partial class DataGrid
|
||||
// or the total remaining amount to adjust has been depleted.
|
||||
foreach (KeyValuePair<DataGridColumn, double> starColumnPair in starColumnPairs)
|
||||
{
|
||||
double distanceToTarget = starColumnPair.Value * starColumnPair.Key.Width.Value;
|
||||
double distanceAvailable = (starColumnPair.Key.Width.Value * remainingAdjustment) / totalStarWeights;
|
||||
double adjustment = increase
|
||||
var distanceToTarget = starColumnPair.Value * starColumnPair.Key.Width.Value;
|
||||
var distanceAvailable = (starColumnPair.Key.Width.Value * remainingAdjustment) / totalStarWeights;
|
||||
var adjustment = increase
|
||||
? Math.Min(distanceToTarget, distanceAvailable)
|
||||
: Math.Max(distanceToTarget, distanceAvailable);
|
||||
|
||||
@@ -948,13 +947,13 @@ public partial class DataGrid
|
||||
|
||||
private bool ComputeDisplayedColumns()
|
||||
{
|
||||
bool invalidate = false;
|
||||
int numVisibleScrollingCols = 0;
|
||||
int visibleScrollingColumnsTmp = 0;
|
||||
double displayWidth = CellsWidth;
|
||||
double cx = 0;
|
||||
int firstDisplayedFrozenCol = -1;
|
||||
int firstDisplayedScrollingCol = DisplayData.FirstDisplayedScrollingCol;
|
||||
var invalidate = false;
|
||||
var numVisibleScrollingCols = 0;
|
||||
var visibleScrollingColumnsTmp = 0;
|
||||
var displayWidth = CellsWidth;
|
||||
var cx = 0d;
|
||||
var firstDisplayedFrozenCol = -1;
|
||||
var firstDisplayedScrollingCol = DisplayData.FirstDisplayedScrollingCol;
|
||||
|
||||
// the same problem with negative numbers:
|
||||
// if the width passed in is negative, then return 0
|
||||
@@ -965,7 +964,7 @@ public partial class DataGrid
|
||||
return invalidate;
|
||||
}
|
||||
|
||||
foreach (DataGridColumn dataGridColumn in ColumnsInternal.GetVisibleFrozenColumns())
|
||||
foreach (var dataGridColumn in ColumnsInternal.GetVisibleFrozenColumns())
|
||||
{
|
||||
if (firstDisplayedFrozenCol == -1)
|
||||
{
|
||||
@@ -983,7 +982,7 @@ public partial class DataGrid
|
||||
|
||||
if (cx < displayWidth && firstDisplayedScrollingCol >= 0)
|
||||
{
|
||||
DataGridColumn? dataGridColumn = ColumnsItemsInternal[firstDisplayedScrollingCol];
|
||||
var dataGridColumn = ColumnsItemsInternal[firstDisplayedScrollingCol];
|
||||
if (dataGridColumn.IsFrozen)
|
||||
{
|
||||
dataGridColumn = ColumnsInternal.FirstVisibleScrollingColumn;
|
||||
@@ -1075,7 +1074,7 @@ public partial class DataGrid
|
||||
visibleScrollingColumnsTmp++;
|
||||
invalidate = true;
|
||||
cx = displayWidth;
|
||||
Debug.Assert(_negHorizontalOffset == GetNegHorizontalOffsetFromHorizontalOffset(_horizontalOffset));
|
||||
Debug.Assert(MathUtils.AreClose(_negHorizontalOffset, GetNegHorizontalOffsetFromHorizontalOffset(_horizontalOffset)));
|
||||
}
|
||||
|
||||
// update the number of visible columns to the new reality
|
||||
@@ -1159,7 +1158,7 @@ public partial class DataGrid
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_negHorizontalOffset != _horizontalOffset)
|
||||
if (!MathUtils.AreClose(_negHorizontalOffset, _horizontalOffset))
|
||||
{
|
||||
_negHorizontalOffset = 0;
|
||||
}
|
||||
@@ -1236,7 +1235,7 @@ public partial class DataGrid
|
||||
// The DisplayIndex of columns greater than the inserted column need to be incremented,
|
||||
// as do the DisplayIndexMap values of modified column Indexes
|
||||
DataGridColumn? column;
|
||||
for (int displayIndex = 0; displayIndex < ColumnsInternal.DisplayIndexMap.Count; displayIndex++)
|
||||
for (var displayIndex = 0; displayIndex < ColumnsInternal.DisplayIndexMap.Count; displayIndex++)
|
||||
{
|
||||
if (ColumnsInternal.DisplayIndexMap[displayIndex] >= insertedColumn.Index)
|
||||
{
|
||||
@@ -1265,10 +1264,10 @@ public partial class DataGrid
|
||||
|
||||
private void CorrectColumnFrozenStates()
|
||||
{
|
||||
int index = 0;
|
||||
double frozenColumnWidth = 0;
|
||||
double oldFrozenColumnWidth = 0;
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetDisplayedColumns())
|
||||
var index = 0;
|
||||
var frozenColumnWidth = 0d;
|
||||
var oldFrozenColumnWidth = 0d;
|
||||
foreach (var column in ColumnsInternal.GetDisplayedColumns())
|
||||
{
|
||||
if (column.IsFrozen)
|
||||
{
|
||||
@@ -1335,7 +1334,7 @@ public partial class DataGrid
|
||||
return amount;
|
||||
}
|
||||
|
||||
double adjustment = Math.Max(
|
||||
var adjustment = Math.Max(
|
||||
column.ActualMinWidth - column.Width.DisplayValue,
|
||||
Math.Max(targetWidth - column.Width.DisplayValue, amount));
|
||||
|
||||
@@ -1363,7 +1362,7 @@ public partial class DataGrid
|
||||
return amount;
|
||||
}
|
||||
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetDisplayedColumns(reverse,
|
||||
foreach (var column in ColumnsInternal.GetDisplayedColumns(reverse,
|
||||
column =>
|
||||
column.IsVisible &&
|
||||
column.Width.UnitType != DataGridLengthUnitType.Star &&
|
||||
@@ -1383,7 +1382,7 @@ public partial class DataGrid
|
||||
|
||||
private void FlushDisplayIndexChanged(bool raiseEvent)
|
||||
{
|
||||
foreach (DataGridColumn column in ColumnsItemsInternal)
|
||||
foreach (var column in ColumnsItemsInternal)
|
||||
{
|
||||
if (column.DisplayIndexHasChanged)
|
||||
{
|
||||
@@ -1415,7 +1414,7 @@ public partial class DataGrid
|
||||
Debug.Assert(ColumnsItemsInternal[index].IsVisible);
|
||||
|
||||
double x = 0;
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetVisibleColumns())
|
||||
foreach (var column in ColumnsInternal.GetVisibleColumns())
|
||||
{
|
||||
if (index == column.Index)
|
||||
{
|
||||
@@ -1430,7 +1429,7 @@ public partial class DataGrid
|
||||
|
||||
private double GetNegHorizontalOffsetFromHorizontalOffset(double horizontalOffset)
|
||||
{
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetVisibleScrollingColumns())
|
||||
foreach (var column in ColumnsInternal.GetVisibleScrollingColumns())
|
||||
{
|
||||
if (GetEdgedColumnWidth(column) > horizontalOffset)
|
||||
{
|
||||
@@ -1462,7 +1461,7 @@ public partial class DataGrid
|
||||
return amount;
|
||||
}
|
||||
|
||||
double adjustment = Math.Min(
|
||||
var adjustment = Math.Min(
|
||||
column.ActualMaxWidth - column.Width.DisplayValue,
|
||||
Math.Min(targetWidth - column.Width.DisplayValue, amount));
|
||||
|
||||
@@ -1490,7 +1489,7 @@ public partial class DataGrid
|
||||
return amount;
|
||||
}
|
||||
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetDisplayedColumns(reverse,
|
||||
foreach (var column in ColumnsInternal.GetDisplayedColumns(reverse,
|
||||
column =>
|
||||
column.IsVisible &&
|
||||
column.Width.UnitType != DataGridLengthUnitType.Star &&
|
||||
@@ -1534,7 +1533,7 @@ public partial class DataGrid
|
||||
|
||||
private void RemoveAutoGeneratedColumns()
|
||||
{
|
||||
int index = 0;
|
||||
var index = 0;
|
||||
_autoGeneratingColumnOperationCount++;
|
||||
try
|
||||
{
|
||||
@@ -1589,15 +1588,15 @@ public partial class DataGrid
|
||||
(DisplayData.LastTotallyDisplayedScrollingCol != columnIndex &&
|
||||
ColumnsInternal.DisplayInOrder(DisplayData.LastTotallyDisplayedScrollingCol, columnIndex)))
|
||||
{
|
||||
double xColumnLeftEdge = GetColumnXFromIndex(columnIndex);
|
||||
double xColumnRightEdge = xColumnLeftEdge + GetEdgedColumnWidth(ColumnsItemsInternal[columnIndex]);
|
||||
double change = xColumnRightEdge - HorizontalOffset - CellsWidth;
|
||||
double widthRemaining = change;
|
||||
var xColumnLeftEdge = GetColumnXFromIndex(columnIndex);
|
||||
var xColumnRightEdge = xColumnLeftEdge + GetEdgedColumnWidth(ColumnsItemsInternal[columnIndex]);
|
||||
var change = xColumnRightEdge - HorizontalOffset - CellsWidth;
|
||||
var widthRemaining = change;
|
||||
|
||||
DataGridColumn newFirstDisplayedScrollingCol =
|
||||
var newFirstDisplayedScrollingCol =
|
||||
ColumnsItemsInternal[DisplayData.FirstDisplayedScrollingCol];
|
||||
DataGridColumn? nextColumn = ColumnsInternal.GetNextVisibleColumn(newFirstDisplayedScrollingCol);
|
||||
double newColumnWidth = GetEdgedColumnWidth(newFirstDisplayedScrollingCol) - _negHorizontalOffset;
|
||||
var nextColumn = ColumnsInternal.GetNextVisibleColumn(newFirstDisplayedScrollingCol);
|
||||
var newColumnWidth = GetEdgedColumnWidth(newFirstDisplayedScrollingCol) - _negHorizontalOffset;
|
||||
while (nextColumn != null && widthRemaining >= newColumnWidth)
|
||||
{
|
||||
widthRemaining -= newColumnWidth;
|
||||
@@ -1639,7 +1638,7 @@ public partial class DataGrid
|
||||
{
|
||||
DataGridColumn? newFirstVisibleScrollingCol = null;
|
||||
DataGridColumn? dataGridColumnTmp;
|
||||
int colCount = 0;
|
||||
var colCount = 0;
|
||||
if (columns > 0)
|
||||
{
|
||||
if (DisplayData.LastTotallyDisplayedScrollingCol >= 0)
|
||||
@@ -1767,36 +1766,35 @@ public partial class DataGrid
|
||||
// Autogenerated Columns are added at the end so the user columns appear first
|
||||
if (DataConnection!.DataProperties != null && DataConnection.DataProperties.Length > 0)
|
||||
{
|
||||
List<KeyValuePair<int, DataGridAutoGeneratingColumnEventArgs>> columnOrderPairs =
|
||||
new List<KeyValuePair<int, DataGridAutoGeneratingColumnEventArgs>>();
|
||||
var columnOrderPairs = new List<KeyValuePair<int, DataGridAutoGeneratingColumnEventArgs>>();
|
||||
|
||||
// Generate the columns
|
||||
foreach (PropertyInfo propertyInfo in DataConnection.DataProperties)
|
||||
{
|
||||
string columnHeader = propertyInfo.Name;
|
||||
int columnOrder = DefaultColumnDisplayOrder;
|
||||
var columnHeader = propertyInfo.Name;
|
||||
var columnOrder = DefaultColumnDisplayOrder;
|
||||
|
||||
// Check if DisplayAttribute is defined on the property
|
||||
object[] attributes = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true);
|
||||
if (attributes != null && attributes.Length > 0)
|
||||
var attributes = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true);
|
||||
if (attributes.Length > 0)
|
||||
{
|
||||
DisplayAttribute? displayAttribute = attributes[0] as DisplayAttribute;
|
||||
var displayAttribute = attributes[0] as DisplayAttribute;
|
||||
Debug.Assert(displayAttribute != null);
|
||||
|
||||
bool? autoGenerateField = displayAttribute.GetAutoGenerateField();
|
||||
var autoGenerateField = displayAttribute.GetAutoGenerateField();
|
||||
if (autoGenerateField.HasValue && autoGenerateField.Value == false)
|
||||
{
|
||||
// Abort column generation because we aren't supposed to auto-generate this field
|
||||
continue;
|
||||
}
|
||||
|
||||
string? header = displayAttribute.GetShortName();
|
||||
var header = displayAttribute.GetShortName();
|
||||
if (header != null)
|
||||
{
|
||||
columnHeader = header;
|
||||
}
|
||||
|
||||
int? order = displayAttribute.GetOrder();
|
||||
var order = displayAttribute.GetOrder();
|
||||
if (order.HasValue)
|
||||
{
|
||||
columnOrder = order.Value;
|
||||
@@ -1804,7 +1802,7 @@ public partial class DataGrid
|
||||
}
|
||||
|
||||
// Generate a single column and determine its relative order
|
||||
int insertIndex = 0;
|
||||
var insertIndex = 0;
|
||||
if (columnOrder == int.MaxValue)
|
||||
{
|
||||
insertIndex = columnOrderPairs.Count;
|
||||
@@ -1845,7 +1843,7 @@ public partial class DataGrid
|
||||
string header)
|
||||
{
|
||||
// Create a new DataBoundColumn for the Property
|
||||
DataGridBoundColumn newColumn = GetDataGridColumnFromType(propertyType);
|
||||
var newColumn = GetDataGridColumnFromType(propertyType);
|
||||
newColumn.Binding = new Binding(propertyName);
|
||||
newColumn.Header = header;
|
||||
newColumn.IsAutoGenerated = true;
|
||||
@@ -1856,22 +1854,15 @@ public partial class DataGrid
|
||||
{
|
||||
// Raise the AutoGeneratingColumn event in case the user wants to Cancel or Replace the
|
||||
// column being generated
|
||||
OnAutoGeneratingColumn(e);
|
||||
HandleAutoGeneratingColumn(e);
|
||||
if (e.Cancel)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (e.Column != null)
|
||||
{
|
||||
// Set the IsAutoGenerated flag here in case the user provides a custom autogenerated column
|
||||
e.Column.IsAutoGenerated = true;
|
||||
}
|
||||
|
||||
ColumnsInternal.Add(e.Column!);
|
||||
ColumnsInternal.AutogeneratedColumnCount++;
|
||||
return true;
|
||||
}
|
||||
// Set the IsAutoGenerated flag here in case the user provides a custom autogenerated column
|
||||
e.Column.IsAutoGenerated = true;
|
||||
ColumnsInternal.Add(e.Column);
|
||||
ColumnsInternal.AutogeneratedColumnCount++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -342,13 +342,13 @@ public partial class DataGrid : TemplatedControl
|
||||
set => SetValue(HeadersVisibilityProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IBrush> HorizontalGridLinesBrushProperty =
|
||||
AvaloniaProperty.Register<DataGrid, IBrush>(nameof(HorizontalGridLinesBrush));
|
||||
public static readonly StyledProperty<IBrush?> HorizontalGridLinesBrushProperty =
|
||||
AvaloniaProperty.Register<DataGrid, IBrush?>(nameof(HorizontalGridLinesBrush));
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="T:System.Windows.Media.Brush" /> that is used to paint grid lines separating rows.
|
||||
/// </summary>
|
||||
public IBrush HorizontalGridLinesBrush
|
||||
public IBrush? HorizontalGridLinesBrush
|
||||
{
|
||||
get => GetValue(HorizontalGridLinesBrushProperty);
|
||||
set => SetValue(HorizontalGridLinesBrushProperty, value);
|
||||
@@ -555,13 +555,13 @@ public partial class DataGrid : TemplatedControl
|
||||
set => SetValue(SelectionModeProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IBrush> VerticalGridLinesBrushProperty =
|
||||
AvaloniaProperty.Register<DataGrid, IBrush>(nameof(VerticalGridLinesBrush));
|
||||
public static readonly StyledProperty<IBrush?> VerticalGridLinesBrushProperty =
|
||||
AvaloniaProperty.Register<DataGrid, IBrush?>(nameof(VerticalGridLinesBrush));
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="T:System.Windows.Media.Brush" /> that is used to paint grid lines separating columns.
|
||||
/// </summary>
|
||||
public IBrush VerticalGridLinesBrush
|
||||
public IBrush? VerticalGridLinesBrush
|
||||
{
|
||||
get => GetValue(VerticalGridLinesBrushProperty);
|
||||
set => SetValue(VerticalGridLinesBrushProperty, value);
|
||||
@@ -697,13 +697,13 @@ public partial class DataGrid : TemplatedControl
|
||||
set => SetValue(AreRowDetailsFrozenProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate> RowDetailsTemplateProperty =
|
||||
AvaloniaProperty.Register<DataGrid, IDataTemplate>(nameof(RowDetailsTemplate));
|
||||
public static readonly StyledProperty<IDataTemplate?> RowDetailsTemplateProperty =
|
||||
AvaloniaProperty.Register<DataGrid, IDataTemplate?>(nameof(RowDetailsTemplate));
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the template that is used to display the content of the details section of rows.
|
||||
/// </summary>
|
||||
public IDataTemplate RowDetailsTemplate
|
||||
public IDataTemplate? RowDetailsTemplate
|
||||
{
|
||||
get => GetValue(RowDetailsTemplateProperty);
|
||||
set => SetValue(RowDetailsTemplateProperty, value);
|
||||
@@ -1835,7 +1835,7 @@ public partial class DataGrid : TemplatedControl
|
||||
/// </summary>
|
||||
/// <param name="editingEventArgs">Provides information about the user gesture that caused the call to BeginEdit. Can be null.</param>
|
||||
/// <returns>True if operation was successful. False otherwise.</returns>
|
||||
public bool BeginEdit(RoutedEventArgs? editingEventArgs = null)
|
||||
public bool BeginEdit(RoutedEventArgs editingEventArgs)
|
||||
{
|
||||
if (CurrentColumnIndex == -1 || !GetRowSelection(CurrentSlot))
|
||||
{
|
||||
@@ -1909,11 +1909,11 @@ public partial class DataGrid : TemplatedControl
|
||||
return;
|
||||
}
|
||||
|
||||
if (item == null)
|
||||
if (item == null && column is not null)
|
||||
{
|
||||
// scroll column into view
|
||||
ScrollSlotIntoView(
|
||||
column!.Index,
|
||||
column.Index,
|
||||
DisplayData.FirstScrollingSlot,
|
||||
forCurrentCellChange: false,
|
||||
forceHorizontalScroll: true);
|
||||
@@ -2463,7 +2463,7 @@ public partial class DataGrid : TemplatedControl
|
||||
|
||||
if (selectedItem != null)
|
||||
{
|
||||
newIndex = DataConnection!.IndexOf(selectedItem);
|
||||
newIndex = DataConnection.IndexOf(selectedItem);
|
||||
}
|
||||
|
||||
SetValueNoCallback(SelectedIndexProperty, newIndex);
|
||||
@@ -2604,13 +2604,18 @@ public partial class DataGrid : TemplatedControl
|
||||
_horizontalScrollChangesIgnored++;
|
||||
try
|
||||
{
|
||||
if (scrollBarValueDifference != 0)
|
||||
// TODO 这样兼容不知道是否合适
|
||||
if (_hScrollBar is not null)
|
||||
{
|
||||
Debug.Assert(_horizontalOffset + scrollBarValueDifference >= 0);
|
||||
_hScrollBar!.Value = _horizontalOffset + scrollBarValueDifference;
|
||||
if (scrollBarValueDifference != 0)
|
||||
{
|
||||
Debug.Assert(_horizontalOffset + scrollBarValueDifference >= 0);
|
||||
_hScrollBar.Value = _horizontalOffset + scrollBarValueDifference;
|
||||
}
|
||||
|
||||
UpdateHorizontalOffset(_hScrollBar.Value);
|
||||
}
|
||||
|
||||
UpdateHorizontalOffset(_hScrollBar!.Value);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -2668,7 +2673,7 @@ public partial class DataGrid : TemplatedControl
|
||||
}
|
||||
else
|
||||
{
|
||||
slot = SlotFromRowIndex(DataConnection!.IndexOf(item));
|
||||
slot = SlotFromRowIndex(DataConnection.IndexOf(item));
|
||||
}
|
||||
|
||||
if (slot == -1)
|
||||
@@ -2768,12 +2773,12 @@ public partial class DataGrid : TemplatedControl
|
||||
//internal void ProcessVerticalScroll(double oldValue, double newValue)
|
||||
internal void ProcessVerticalScroll(ScrollEventType scrollEventType)
|
||||
{
|
||||
if (_verticalScrollChangesIgnored > 0)
|
||||
if (_verticalScrollChangesIgnored > 0 || _vScrollBar is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Assert(MathUtilities.LessThanOrClose(_vScrollBar!.Value, _vScrollBar.Maximum));
|
||||
|
||||
Debug.Assert(MathUtilities.LessThanOrClose(_vScrollBar.Value, _vScrollBar.Maximum));
|
||||
|
||||
_verticalScrollChangesIgnored++;
|
||||
try
|
||||
@@ -2848,7 +2853,7 @@ public partial class DataGrid : TemplatedControl
|
||||
//We don't need to refresh the state of AutoGenerated column headers because they're up-to-date
|
||||
if (!column.IsAutoGenerated && column.HasHeaderCell)
|
||||
{
|
||||
column.HeaderCell!.UpdatePseudoClasses();
|
||||
column.HeaderCell?.UpdatePseudoClasses();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2961,18 +2966,19 @@ public partial class DataGrid : TemplatedControl
|
||||
return false;
|
||||
}
|
||||
|
||||
int newCurrentPosition = -1;
|
||||
object? item = ItemFromSlot(slot, ref newCurrentPosition);
|
||||
int newCurrentPosition = -1;
|
||||
var item = ItemFromSlot(slot, ref newCurrentPosition);
|
||||
|
||||
if (EditingRow != null && slot != EditingRow.Slot && !CommitEdit(DataGridEditingUnit.Row, true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (DataConnection!.CollectionView != null &&
|
||||
DataConnection.CollectionView.CurrentPosition != newCurrentPosition)
|
||||
if (DataConnection.CollectionView != null &&
|
||||
DataConnection.CollectionView.CurrentPosition != newCurrentPosition &&
|
||||
item is not null)
|
||||
{
|
||||
DataConnection.MoveCurrentTo(item!, slot, columnIndex, action, scrollIntoView);
|
||||
DataConnection.MoveCurrentTo(item, slot, columnIndex, action, scrollIntoView);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -3227,7 +3233,7 @@ public partial class DataGrid : TemplatedControl
|
||||
row.Cells.Insert(column.Index, newCell);
|
||||
}
|
||||
|
||||
private bool BeginCellEdit(RoutedEventArgs? editingEventArgs)
|
||||
private bool BeginCellEdit(RoutedEventArgs editingEventArgs)
|
||||
{
|
||||
if (CurrentColumnIndex == -1 || !GetRowSelection(CurrentSlot))
|
||||
{
|
||||
@@ -3239,8 +3245,9 @@ public partial class DataGrid : TemplatedControl
|
||||
Debug.Assert(CurrentSlot >= -1);
|
||||
Debug.Assert(CurrentSlot < SlotCount);
|
||||
Debug.Assert(EditingRow == null || EditingRow.Slot == CurrentSlot);
|
||||
Debug.Assert(!GetColumnEffectiveReadOnlyState(CurrentColumn!));
|
||||
Debug.Assert(CurrentColumn!.IsVisible);
|
||||
Debug.Assert(CurrentColumn is not null);
|
||||
Debug.Assert(!GetColumnEffectiveReadOnlyState(CurrentColumn));
|
||||
Debug.Assert(CurrentColumn.IsVisible);
|
||||
|
||||
if (_editingColumnIndex != -1)
|
||||
{
|
||||
@@ -3272,7 +3279,7 @@ public partial class DataGrid : TemplatedControl
|
||||
|
||||
// Raise the BeginningEdit event
|
||||
var dataGridCell = dataGridRow.Cells[CurrentColumnIndex];
|
||||
var e = new DataGridBeginningEditEventArgs(CurrentColumn, dataGridRow, editingEventArgs!);
|
||||
var e = new DataGridBeginningEditEventArgs(CurrentColumn, dataGridRow, editingEventArgs);
|
||||
HandleBeginningEdit(e);
|
||||
if (e.Cancel
|
||||
|| currentRowIndex != CurrentSlot
|
||||
@@ -3331,12 +3338,12 @@ public partial class DataGrid : TemplatedControl
|
||||
Debug.Assert(CurrentColumn != null);
|
||||
|
||||
var dataItem = EditingRow.DataContext;
|
||||
if (!DataConnection.CancelEdit(dataItem!))
|
||||
if (dataItem is null || !DataConnection.CancelEdit(dataItem))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (DataGridColumn column in Columns)
|
||||
foreach (var column in Columns)
|
||||
{
|
||||
if (!exitEditingMode && column.Index == _editingColumnIndex && column is DataGridBoundColumn)
|
||||
{
|
||||
@@ -3412,7 +3419,7 @@ public partial class DataGrid : TemplatedControl
|
||||
return false;
|
||||
}
|
||||
|
||||
DataConnection!.EndEdit(EditingRow.DataContext!);
|
||||
DataConnection.EndEdit(EditingRow.DataContext);
|
||||
|
||||
if (!exitEditingMode)
|
||||
{
|
||||
@@ -4047,7 +4054,7 @@ public partial class DataGrid : TemplatedControl
|
||||
// If we're canceling, let the editing column repopulate its old value if it wants
|
||||
if (editAction == DataGridEditAction.Cancel)
|
||||
{
|
||||
CurrentColumn!.CancelCellEditInternal(editingElement, _uneditedValue!);
|
||||
CurrentColumn?.CancelCellEditInternal(editingElement, _uneditedValue!);
|
||||
|
||||
// Ensure that the current cell wasn't changed in the user column's CancelCellEdit
|
||||
if (_editingColumnIndex == -1 ||
|
||||
@@ -4109,7 +4116,7 @@ public partial class DataGrid : TemplatedControl
|
||||
|
||||
if (exitEditingMode)
|
||||
{
|
||||
CurrentColumn!.EndCellEditInternal();
|
||||
CurrentColumn?.EndCellEditInternal();
|
||||
_editingColumnIndex = -1;
|
||||
editingCell.UpdatePseudoClasses();
|
||||
|
||||
@@ -4121,17 +4128,17 @@ public partial class DataGrid : TemplatedControl
|
||||
|
||||
PopulateCellContent(
|
||||
isCellEdited: !exitEditingMode,
|
||||
dataGridColumn: CurrentColumn,
|
||||
dataGridColumn: CurrentColumn!,
|
||||
dataGridRow: editingRow,
|
||||
dataGridCell: editingCell);
|
||||
|
||||
editingRow.InvalidateDesiredHeight();
|
||||
var column = editingCell.OwningColumn;
|
||||
if (column!.Width.IsSizeToCells || column.Width.IsAuto)
|
||||
var column = editingCell.OwningColumn!;
|
||||
if (column.Width.IsSizeToCells || column.Width.IsAuto)
|
||||
{
|
||||
// Invalidate desired width and force recalculation
|
||||
column.SetWidthDesiredValue(0);
|
||||
editingRow.OwningGrid!.AutoSizeColumn(column, editingCell.DesiredSize.Width);
|
||||
editingRow.OwningGrid?.AutoSizeColumn(column, editingCell.DesiredSize.Width);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4149,7 +4156,7 @@ public partial class DataGrid : TemplatedControl
|
||||
//TODO Validation
|
||||
private bool EndRowEdit(DataGridEditAction editAction, bool exitEditingMode, bool raiseEvents)
|
||||
{
|
||||
if (EditingRow == null || DataConnection!.CommittingEdit)
|
||||
if (EditingRow == null || DataConnection.CommittingEdit)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -4239,7 +4246,7 @@ public partial class DataGrid : TemplatedControl
|
||||
if (AreColumnHeadersVisible)
|
||||
{
|
||||
double totalColumnsWidth = 0;
|
||||
foreach (DataGridColumn column in ColumnsInternal)
|
||||
foreach (var column in ColumnsInternal)
|
||||
{
|
||||
totalColumnsWidth += column.ActualWidth;
|
||||
|
||||
@@ -4261,7 +4268,7 @@ public partial class DataGrid : TemplatedControl
|
||||
//TODO TabStop
|
||||
private void ExitEdit(bool keepFocus)
|
||||
{
|
||||
if (EditingRow == null || DataConnection!.CommittingEdit)
|
||||
if (EditingRow == null || DataConnection.CommittingEdit)
|
||||
{
|
||||
Debug.Assert(_editingColumnIndex == -1);
|
||||
return;
|
||||
@@ -4493,7 +4500,7 @@ public partial class DataGrid : TemplatedControl
|
||||
return;
|
||||
}
|
||||
|
||||
if (SlotCount != SlotFromRowIndex(DataConnection!.Count))
|
||||
if (SlotCount != SlotFromRowIndex(DataConnection.Count))
|
||||
{
|
||||
_makeFirstDisplayedCellCurrentCellPending = true;
|
||||
return;
|
||||
@@ -4602,8 +4609,8 @@ public partial class DataGrid : TemplatedControl
|
||||
|
||||
// Prepare the cell for editing and raise the PreparingCellForEdit event for all columns
|
||||
var dataGridColumn = CurrentColumn;
|
||||
_uneditedValue = dataGridColumn!.PrepareCellForEditInternal(editingElement!, _editingEventArgs!);
|
||||
HandlePreparingCellForEdit(new DataGridPreparingCellForEditEventArgs(dataGridColumn, EditingRow,
|
||||
_uneditedValue = dataGridColumn?.PrepareCellForEditInternal(editingElement!, _editingEventArgs!);
|
||||
HandlePreparingCellForEdit(new DataGridPreparingCellForEditEventArgs(dataGridColumn!, EditingRow,
|
||||
_editingEventArgs!,
|
||||
editingElement!));
|
||||
}
|
||||
@@ -4963,7 +4970,7 @@ public partial class DataGrid : TemplatedControl
|
||||
if (CurrentColumnIndex != -1)
|
||||
{
|
||||
dataGridColumn =
|
||||
ColumnsInternal!.GetPreviousVisibleNonFillerColumn(ColumnsItemsInternal[CurrentColumnIndex]);
|
||||
ColumnsInternal.GetPreviousVisibleNonFillerColumn(ColumnsItemsInternal[CurrentColumnIndex]);
|
||||
if (dataGridColumn != null)
|
||||
{
|
||||
previousVisibleColumnIndex = dataGridColumn.Index;
|
||||
@@ -5459,9 +5466,9 @@ public partial class DataGrid : TemplatedControl
|
||||
|
||||
private void RemoveDisplayedColumnHeader(DataGridColumn dataGridColumn)
|
||||
{
|
||||
if (_columnHeadersPresenter != null)
|
||||
if (_columnHeadersPresenter != null && dataGridColumn.HeaderCell is not null)
|
||||
{
|
||||
_columnHeadersPresenter.Children.Remove(dataGridColumn.HeaderCell!);
|
||||
_columnHeadersPresenter.Children.Remove(dataGridColumn.HeaderCell);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5551,7 +5558,7 @@ public partial class DataGrid : TemplatedControl
|
||||
if (!RowGroupHeadersTable.Contains(slot))
|
||||
{
|
||||
int rowIndex = RowIndexFromSlot(slot);
|
||||
if (rowIndex >= 0 && rowIndex < DataConnection!.Count)
|
||||
if (rowIndex >= 0 && rowIndex < DataConnection.Count)
|
||||
{
|
||||
newCurrentItem = DataConnection.GetDataItem(rowIndex);
|
||||
}
|
||||
@@ -5601,7 +5608,7 @@ public partial class DataGrid : TemplatedControl
|
||||
|
||||
if (newCurrentItem != null)
|
||||
{
|
||||
slot = SlotFromRowIndex(DataConnection!.IndexOf(newCurrentItem));
|
||||
slot = SlotFromRowIndex(DataConnection.IndexOf(newCurrentItem));
|
||||
}
|
||||
|
||||
if (slot == -1 && columnIndex != -1)
|
||||
@@ -6124,7 +6131,7 @@ public partial class DataGrid : TemplatedControl
|
||||
if (ClipboardCopyMode == DataGridClipboardCopyMode.IncludeHeader)
|
||||
{
|
||||
DataGridRowClipboardEventArgs headerArgs = new DataGridRowClipboardEventArgs(null, true);
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetVisibleColumns())
|
||||
foreach (var column in ColumnsInternal.GetVisibleColumns())
|
||||
{
|
||||
headerArgs.ClipboardRowContent.Add(new DataGridClipboardCellContent(null, column, column.Header!));
|
||||
}
|
||||
@@ -6133,13 +6140,13 @@ public partial class DataGrid : TemplatedControl
|
||||
textBuilder.Append(FormatClipboardContent(headerArgs));
|
||||
}
|
||||
|
||||
for (int index = 0; index < SelectedItems.Count; index++)
|
||||
for (var index = 0; index < SelectedItems.Count; index++)
|
||||
{
|
||||
object? item = SelectedItems[index];
|
||||
DataGridRowClipboardEventArgs itemArgs = new DataGridRowClipboardEventArgs(item!, false);
|
||||
foreach (DataGridColumn column in ColumnsInternal.GetVisibleColumns())
|
||||
{
|
||||
object? content = column.GetCellValue(item!, column.ClipboardContentBinding);
|
||||
var content = column.GetCellValue(item!, column.ClipboardContentBinding);
|
||||
itemArgs.ClipboardRowContent.Add(new DataGridClipboardCellContent(item!, column, content!));
|
||||
}
|
||||
|
||||
@@ -6147,7 +6154,7 @@ public partial class DataGrid : TemplatedControl
|
||||
textBuilder.Append(FormatClipboardContent(itemArgs));
|
||||
}
|
||||
|
||||
string text = StringBuilderCache.GetStringAndRelease(textBuilder);
|
||||
var text = StringBuilderCache.GetStringAndRelease(textBuilder);
|
||||
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user