Skip to content

Commit 8853b34

Browse files
author
Anjali Sheel
authored
Adding ability to resize data grid and grid view using keyboard shortcut (#6812)
1 parent 47a8bd8 commit 8853b34

File tree

4 files changed

+79
-56
lines changed

4 files changed

+79
-56
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -964,28 +964,36 @@ 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))
967+
if (!e.Handled)
968968
{
969-
DataGridLength updatedWidth = new DataGridLength();
969+
const ModifierKeys ModifierMask = ModifierKeys.Alt | ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Windows;
970+
ModifierKeys modifierKeys = Keyboard.Modifiers & ModifierMask;
970971

971-
if (e.Key == Key.Right)
972+
if (((e.SystemKey == Key.Right) || (e.SystemKey == Key.Left)) && (modifierKeys == ModifierKeys.Alt))
972973
{
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-
}
974+
DataGridLength updatedWidth = new DataGridLength();
979975

980-
if (Column.CanColumnResize(updatedWidth))
981-
{
982-
this.Column.SetCurrentValue(DataGridColumn.WidthProperty, updatedWidth);
983-
}
976+
if (e.SystemKey == Key.Right)
977+
{
978+
updatedWidth = new DataGridLength(Column.ActualWidth + ColumnWidthStepSize);
979+
}
980+
else if (e.SystemKey == Key.Left)
981+
{
982+
updatedWidth = new DataGridLength(Column.ActualWidth - ColumnWidthStepSize);
983+
}
984984

985-
e.Handled = true;
986-
return;
985+
if (Column != null)
986+
{
987+
if (Column.CanColumnResize(updatedWidth))
988+
{
989+
Column.SetCurrentValueInternal(DataGridColumn.WidthProperty, updatedWidth);
990+
}
991+
e.Handled = true;
992+
}
993+
return;
994+
}
987995
}
988-
996+
989997
SendInputToColumn(e);
990998
}
991999

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ private static object OnCoerceCanUserResize(DependencyObject d, object baseValue
14911491
column.DataGridOwner,
14921492
DataGrid.CanUserResizeColumnsProperty);
14931493
}
1494-
1494+
14951495
internal bool CanColumnResize(DataGridLength width)
14961496
{
14971497
if (!CanUserResize)
@@ -1501,7 +1501,7 @@ internal bool CanColumnResize(DataGridLength width)
15011501

15021502
return width.DisplayValue >= this.MinWidth && width.DisplayValue <= this.MaxWidth;
15031503
}
1504-
1504+
15051505
#endregion
15061506

15071507
#region Hidden Columns

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,19 @@ internal void UpdateProperty(DependencyProperty dp, object value)
452452
SetFlag(ignoreFlag, false);
453453
}
454454

455+
// Set column header width and associated column width
456+
internal void UpdateColumnHeaderWidth(double width)
457+
{
458+
if (Column != null)
459+
{
460+
Column.Width = width;
461+
}
462+
else
463+
{
464+
Width = width;
465+
}
466+
}
467+
455468
#endregion Internal Methods
456469

457470
//-------------------------------------------------------------------
@@ -788,19 +801,6 @@ private void UpdateGripperCursor()
788801
}
789802
}
790803

791-
// Set column header width and associated column width
792-
internal void UpdateColumnHeaderWidth(double width)
793-
{
794-
if (Column != null)
795-
{
796-
Column.Width = width;
797-
}
798-
else
799-
{
800-
Width = width;
801-
}
802-
}
803-
804804
private bool IsMouseOutside()
805805
{
806806
Point pos = Mouse.PrimaryDevice.GetPosition(this);

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

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -349,31 +349,7 @@ protected override void OnKeyDown(KeyEventArgs e)
349349
case Key.Left:
350350
case Key.Down:
351351
case Key.Right:
352-
{
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-
352+
{
377353
KeyboardNavigation.ShowFocusVisual();
378354

379355
// Depend on logical orientation we decide to move focus or just scroll
@@ -509,6 +485,45 @@ protected override void OnKeyDown(KeyEventArgs e)
509485
NavigateByPage(FocusNavigationDirection.Down, new ItemNavigateArgs(e.Device, Keyboard.Modifiers));
510486
break;
511487

488+
case Key.System:
489+
Key skey = e.SystemKey;
490+
switch (skey)
491+
{
492+
case Key.Right:
493+
case Key.Left:
494+
const ModifierKeys ModifierMask = ModifierKeys.Alt | ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Windows;
495+
ModifierKeys modifierKeys = Keyboard.Modifiers & ModifierMask;
496+
497+
if (modifierKeys == ModifierKeys.Alt)
498+
{
499+
if (e.OriginalSource is GridViewColumnHeader gridViewColumnHeader && gridViewColumnHeader.Column != null)
500+
{
501+
double width = 0;
502+
if (e.SystemKey == Key.Left)
503+
{
504+
width = gridViewColumnHeader.Column.ActualWidth - ColumnWidthStepSize;
505+
}
506+
else if (e.SystemKey == Key.Right)
507+
{
508+
width = gridViewColumnHeader.Column.ActualWidth + ColumnWidthStepSize;
509+
}
510+
511+
if (width > 0)
512+
{
513+
gridViewColumnHeader.UpdateColumnHeaderWidth(width);
514+
}
515+
}
516+
}
517+
break;
518+
519+
default:
520+
handled = false;
521+
break;
522+
}
523+
524+
break;
525+
526+
512527
default:
513528
handled = false;
514529
break;

0 commit comments

Comments
 (0)