Skip to content

Commit 1e45cc3

Browse files
committed
fix grid keyboard navigation (wpf nightmare part 7394)
1 parent 95d4b3c commit 1e45cc3

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

UnityLauncherPro/MainWindow.xaml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
</Style.Triggers>
8787
</Style>-->
8888

89+
<Style x:Key="NoFocusCellStyle" TargetType="DataGridCell">
90+
<Setter Property="Focusable" Value="False"/>
91+
</Style>
92+
8993
<!-- custom buttons -->
9094
<Style x:Key="CustomButton" TargetType="Button">
9195
<Setter Property="SnapsToDevicePixels" Value="true"/>
@@ -221,6 +225,8 @@
221225

222226
</Window.Resources>
223227

228+
229+
224230

225231
<!-- UI -->
226232

@@ -291,9 +297,9 @@
291297
<Button Style="{StaticResource CustomButton}" ToolTip="Add existing project" x:Name="btnAddProjectFolder" Content="Add Project.." Height="22" Width="78" HorizontalAlignment="Right" VerticalAlignment="Top" Background="#FF3F3F46" Foreground="#FFC1C1C1" Margin="0,4,34,0" Click="BtnAddProjectFolder_Click" BorderBrush="{x:Null}" />
292298
<Button Style="{StaticResource CustomButton}" ToolTip="Refresh list (F5)" x:Name="btnRefreshProjectList" Content="" Height="22" Width="22" HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="16" Background="#FF3F3F46" Foreground="#FFC1C1C1" Margin="0,4,3,0" Padding="1,-2,1,1" BorderBrush="{x:Null}" Click="BtnRefreshProjectList_Click"/>
293299

294-
<DataGrid x:Name="gridRecent" SelectionMode="Single" KeyboardNavigation.TabNavigation="Once" Margin="4,30,2,42" Background="{x:Null}" BorderBrush="{x:Null}" ColumnHeaderStyle="{StaticResource HeaderStyle}" Padding="0" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column" Foreground="#FFD8D8D8" HorizontalGridLinesBrush="#4C000000" VerticalGridLinesBrush="#19000000" AutoGenerateColumns="False" PreviewKeyDown="GridRecent_PreviewKeyDown">
300+
<DataGrid x:Name="gridRecent" SelectionMode="Single" Margin="4,30,2,42" Background="{x:Null}" BorderBrush="{x:Null}" ColumnHeaderStyle="{StaticResource HeaderStyle}" Padding="0" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column" Foreground="#FFD8D8D8" HorizontalGridLinesBrush="#4C000000" VerticalGridLinesBrush="#19000000" AutoGenerateColumns="False" PreviewKeyDown="GridRecent_PreviewKeyDown" Loaded="GridRecent_Loaded">
295301
<DataGrid.Columns>
296-
<DataGridTextColumn Binding="{Binding Title}" ClipboardContentBinding="{x:Null}" Header="Project" IsReadOnly="True" Width="150"/>
302+
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Title}" ClipboardContentBinding="{x:Null}" Header="Project" IsReadOnly="True" Width="150"/>
297303
<DataGridTextColumn Binding="{Binding Version}" ClipboardContentBinding="{x:Null}" Header="Version" IsReadOnly="True" Width="72">
298304
<DataGridTextColumn.CellStyle>
299305
<Style TargetType="{x:Type DataGridCell}">
@@ -306,10 +312,10 @@
306312
</Style>
307313
</DataGridTextColumn.CellStyle>
308314
</DataGridTextColumn>
309-
<DataGridTextColumn Binding="{Binding Path}" ClipboardContentBinding="{x:Null}" Header="Path" IsReadOnly="True" Width="185"/>
310-
<DataGridTextColumn Binding="{Binding Modified, StringFormat=\{0:dd/MM/yyyy HH:mm:ss\}}" ClipboardContentBinding="{x:Null}" Header="Modified" IsReadOnly="True" Width="120"/>
311-
<DataGridTextColumn Binding="{Binding Arguments}" ClipboardContentBinding="{x:Null}" Header="Arguments" IsReadOnly="True" Width="100"/>
312-
<DataGridTextColumn Binding="{Binding GITBranch}" ClipboardContentBinding="{x:Null}" Header="GITBranch" IsReadOnly="True" Width="100"/>
315+
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Path}" ClipboardContentBinding="{x:Null}" Header="Path" IsReadOnly="True" Width="185"/>
316+
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Modified, StringFormat=\{0:dd/MM/yyyy HH:mm:ss\}}" ClipboardContentBinding="{x:Null}" Header="Modified" IsReadOnly="True" Width="120"/>
317+
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding Arguments}" ClipboardContentBinding="{x:Null}" Header="Arguments" IsReadOnly="True" Width="100"/>
318+
<DataGridTextColumn CellStyle="{StaticResource NoFocusCellStyle}" Binding="{Binding GITBranch}" ClipboardContentBinding="{x:Null}" Header="GITBranch" IsReadOnly="True" Width="100"/>
313319
</DataGrid.Columns>
314320

315321
<!-- right click context menu -->

UnityLauncherPro/MainWindow.xaml.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ void Start()
7373
notifyIcon.Icon = new Icon(System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Images/icon.ico")).Stream);
7474
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseClick);
7575

76-
gridRecent.Focus();
77-
gridRecent.SelectedIndex = 0;
7876

77+
//gridRecent.CurrentCell = gridRecent.sele;
7978
}
8079

8180
void NotifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
@@ -206,7 +205,6 @@ private void OnWindowKeyDown(object sender, KeyEventArgs e)
206205
{
207206
// TODO if editing cells, dont focus on search
208207
//if (gridRecent.IsCurrentCellInEditMode == true) return;
209-
210208
switch (e.Key)
211209
{
212210
case Key.Escape: // clear project search
@@ -534,11 +532,19 @@ void DisplayUpgradeDialog(Project proj)
534532
// need to manually move into next/prev rows? https://stackoverflow.com/a/11652175/5452781
535533
private void GridRecent_PreviewKeyDown(object sender, KeyEventArgs e)
536534
{
537-
Tools.HandleDataGridScrollKeys(sender, e);
535+
//Tools.HandleDataGridScrollKeys(sender, e);
538536
}
539537

540-
538+
private void GridRecent_Loaded(object sender, RoutedEventArgs e)
539+
{
540+
gridRecent.Focus();
541+
gridRecent.SelectedIndex = 0;
542+
// properly set focus to row
543+
DataGridRow row = (DataGridRow)gridRecent.ItemContainerGenerator.ContainerFromIndex(0);
544+
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
545+
}
541546
} // class
542547
} //namespace
543548

544549

550+

UnityLauncherPro/Tools.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ public static KeyValuePair<TKey, TValue> GetEntry<TKey, TValue>(this IDictionary
372372

373373
public static void HandleDataGridScrollKeys(object sender, KeyEventArgs e)
374374
{
375+
/*
375376
DataGrid grid = sender as DataGrid;
376377
switch (e.Key)
377378
{
@@ -380,19 +381,25 @@ public static void HandleDataGridScrollKeys(object sender, KeyEventArgs e)
380381
{
381382
grid.SelectedIndex--;
382383
}
383-
else
384-
{
385-
grid.SelectedIndex = grid.Items.Count - 1;
386-
}
387-
e.Handled = true;
388-
break;
384+
// disable wrap around
385+
386+
//else
387+
//{
388+
// grid.SelectedIndex = grid.Items.Count - 1;
389+
//}
390+
e.Handled = true;
391+
break;
389392
case Key.Down:
390-
grid.SelectedIndex = ++grid.SelectedIndex % grid.Items.Count;
391-
e.Handled = true;
392-
break;
393+
if (grid.SelectedIndex < grid.Items.Count)
394+
{
395+
grid.SelectedIndex++;
393396
}
394-
grid.ScrollIntoView(grid.Items[grid.SelectedIndex]);
397+
//grid.SelectedIndex = ++grid.SelectedIndex % grid.Items.Count;
398+
e.Handled = true;
399+
break;
400+
}
401+
grid.ScrollIntoView(grid.Items[grid.SelectedIndex]);
402+
*/
395403
}
396-
397404
} // class
398405
} // namespace

0 commit comments

Comments
 (0)