Skip to content

Commit

Permalink
Improve DataGrid so that single-clicking a cell immediately enters Ed…
Browse files Browse the repository at this point in the history
…it Mode + add "EnableTwoStepsEditMode" option to revert to the previous behavior (which was closer to the Silverlight DataGrid behavior)
  • Loading branch information
cshtml5 committed Jun 10, 2019
1 parent 19b3593 commit 8c1a50b
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/CSHTML5.Runtime/Windows.UI.Xaml.Controls/DataGrid.cs
Expand Up @@ -297,6 +297,24 @@ private static void IsReadonly_Changed(DependencyObject d, DependencyPropertyCha
//todo: manage this
}

/// <summary>
/// Gets or sets a value that indicates whether entering Edit Mode requires two steps
/// (like in Silverlight) rather than one. 'True' means that the user must first select
/// a cell and then click again to enter Edit Mode. 'False' means that the DataGrid
/// enters Edit Mode immediately when a cell is clicked, even if the cell is not
/// selected yet (it will get selected too). Default is false.
/// </summary>
public bool EnableTwoStepsEditMode
{
get { return (bool)GetValue(EnableTwoStepsEditModeProperty); }
set { SetValue(EnableTwoStepsEditModeProperty, value); }
}

public static readonly DependencyProperty EnableTwoStepsEditModeProperty =
DependencyProperty.Register("EnableTwoStepsEditMode", typeof(bool), typeof(DataGrid), new PropertyMetadata(false));



/// <summary>
/// Gets or sets a value that indicates how rows and cells are selected in the
/// System.Windows.Controls.DataGrid.
Expand Down Expand Up @@ -899,6 +917,10 @@ void CellElement_Click(object sender, RoutedEventArgs e)
DataGridCell cell = (DataGridCell)sender;
if (_currentCell != cell)
{
//-----------------------
// The clicked cell was not already selected
//-----------------------

if (SelectionMode == DataGridSelectionMode.Single && SelectedItem != null)
{
UnselectItem(SelectedItem);
Expand All @@ -909,10 +931,21 @@ void CellElement_Click(object sender, RoutedEventArgs e)
}
SelectItem(cell.Item);
_currentCell = cell;

// Enter Edit Mode (upon selection) only if the option "EnableTwoStepsEditMode" is not enabled (otherwise the user must first select the cell, like in Silverlight):
if (!EnableTwoStepsEditMode)
{
EnterCellEditionMode(cell);
}
}
else
{
EnterCellEditionMode(cell); //if _currentCell == cell, it means that the cell clicked was already selected.
//-----------------------
// The clicked cell was already selected
//-----------------------

// Enter Edit Mode:
EnterCellEditionMode(cell);
}
}

Expand Down

0 comments on commit 8c1a50b

Please sign in to comment.