Skip to content

Commit d185380

Browse files
author
Anjali Sheel
authored
WPF DataGrid/GridView column width can be changed using the keyboard shortcut ALT+left or right arrow key (#6054)
* Update DataGridCell.cs * Update DataGridColumn.cs * Update DataGridCell.cs 11383796 - Renamed variable name as per PascalCasing. * Made UpdateColumnHeaderWidth function to internal Made UpdateColumnHeaderWidth function from private to internal. Earlier scope of this function was limited to this file, now for the grid view changes, we want access for this function in the ListBox.cs * Added column resize support for GridView using keyboard * Incorporated review changes * Incorporated review changes
1 parent ee5e134 commit d185380

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridCell.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,28 @@ protected override void OnPreviewKeyDown(KeyEventArgs e)
964964
/// </summary>
965965
protected override void OnKeyDown(KeyEventArgs e)
966966
{
967+
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt && (e.Key == Key.Left || e.Key == Key.Right))
968+
{
969+
DataGridLength updatedWidth = new DataGridLength();
970+
971+
if (e.Key == Key.Right)
972+
{
973+
updatedWidth = new DataGridLength(this.Column.ActualWidth + ColumnWidthStepSize);
974+
}
975+
else if (e.Key == Key.Left)
976+
{
977+
updatedWidth = new DataGridLength(this.Column.ActualWidth - ColumnWidthStepSize);
978+
}
979+
980+
if (Column.CanColumnResize(updatedWidth))
981+
{
982+
this.Column.SetCurrentValue(DataGridColumn.WidthProperty, updatedWidth);
983+
}
984+
985+
e.Handled = true;
986+
return;
987+
}
988+
967989
SendInputToColumn(e);
968990
}
969991

@@ -1100,6 +1122,7 @@ private bool NeedsVisualTree
11001122
private DataGridRow _owner;
11011123
private ContainerTracking<DataGridCell> _tracker;
11021124
private bool _syncingIsSelected; // Used to prevent unnecessary notifications
1125+
private const double ColumnWidthStepSize = 10d;
11031126

11041127
#endregion
11051128
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumn.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,17 @@ private static object OnCoerceCanUserResize(DependencyObject d, object baseValue
14911491
column.DataGridOwner,
14921492
DataGrid.CanUserResizeColumnsProperty);
14931493
}
1494+
1495+
internal bool CanColumnResize(DataGridLength width)
1496+
{
1497+
if (!CanUserResize)
1498+
{
1499+
return false;
1500+
}
14941501

1502+
return width.DisplayValue >= this.MinWidth && width.DisplayValue <= this.MaxWidth;
1503+
}
1504+
14951505
#endregion
14961506

14971507
#region Hidden Columns

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GridViewColumnHeader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ private void UpdateGripperCursor()
789789
}
790790

791791
// Set column header width and associated column width
792-
private void UpdateColumnHeaderWidth(double width)
792+
internal void UpdateColumnHeaderWidth(double width)
793793
{
794794
if (Column != null)
795795
{
@@ -979,7 +979,7 @@ private enum Flags
979979
// Define a Style with ContentTemplate and assign it to GridViewColumn.HeaderContainerStyle property. GridViewColumnHeader.OnPropertyChagned method will be called twice.
980980
// The first call is for ContentTemplate property. In this call, IgnoreContentTemplate is false.
981981
// The second call is for Style property. In this call, IgnoreStyle is true.
982-
// One flag can’t distinguish them.
982+
// One flag can’t distinguish them.
983983
None = 0,
984984
StyleSetByUser = 0x00000001,
985985
IgnoreStyle = 0x00000002,

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ListBox.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,30 @@ protected override void OnKeyDown(KeyEventArgs e)
350350
case Key.Down:
351351
case Key.Right:
352352
{
353+
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt && (e.Key == Key.Left || e.Key == Key.Right))
354+
{
355+
if(e.OriginalSource is GridViewColumnHeader gridViewColumnHeader)
356+
{
357+
if (key == Key.Left)
358+
{
359+
if(gridViewColumnHeader.Column.ActualWidth > 0)
360+
{
361+
gridViewColumnHeader.Width = gridViewColumnHeader.Column.ActualWidth - ColumnWidthStepSize;
362+
gridViewColumnHeader.UpdateColumnHeaderWidth(gridViewColumnHeader.Width);
363+
}
364+
365+
handled = true;
366+
}
367+
else if (key == Key.Right)
368+
{
369+
gridViewColumnHeader.Width = gridViewColumnHeader.Column.ActualWidth + ColumnWidthStepSize;
370+
gridViewColumnHeader.UpdateColumnHeaderWidth(gridViewColumnHeader.Width);
371+
handled = true;
372+
}
373+
break;
374+
}
375+
}
376+
353377
KeyboardNavigation.ShowFocusVisual();
354378

355379
// Depend on logical orientation we decide to move focus or just scroll
@@ -1011,6 +1035,8 @@ internal ListBoxItem LastActionItem
10111035
private WeakReference _lastActionItem;
10121036

10131037
private DispatcherTimer _autoScrollTimer;
1038+
1039+
private const double ColumnWidthStepSize = 10d;
10141040

10151041
private static RoutedUICommand SelectAllCommand =
10161042
new RoutedUICommand(SR.Get(SRID.ListBoxSelectAllText), "SelectAll", typeof(ListBox));

0 commit comments

Comments
 (0)