|
17 | 17 | using System.Windows.Controls;
|
18 | 18 | using System.Windows.Input;
|
19 | 19 | using System.Windows.Media;
|
| 20 | +using System.Windows.Threading; |
20 | 21 | using UnityLauncherPro.Helpers;
|
21 | 22 |
|
22 | 23 | namespace UnityLauncherPro
|
@@ -1592,47 +1593,39 @@ public static bool HasFocus(DependencyObject obj, Control control, bool checkChi
|
1592 | 1593 |
|
1593 | 1594 | public static void SetFocusToGrid(DataGrid targetGrid, int index = -1)
|
1594 | 1595 | {
|
1595 |
| - // set main component focus |
1596 |
| - //targetGrid.Focus(); |
1597 |
| - //Keyboard.Focus(targetGrid); |
1598 |
| - |
1599 |
| - // no items |
1600 | 1596 | if (targetGrid.Items.Count < 1) return;
|
1601 | 1597 |
|
1602 |
| - // keep current row selected |
1603 | 1598 | if (index == -1 && targetGrid.SelectedIndex > -1) index = targetGrid.SelectedIndex;
|
1604 |
| - |
1605 |
| - // if no item selected, pick first |
1606 | 1599 | if (index == -1) index = 0;
|
1607 | 1600 |
|
1608 | 1601 | targetGrid.SelectedIndex = index;
|
1609 | 1602 |
|
1610 |
| - // set full focus |
| 1603 | + // Try get the row, if not realized yet, defer |
1611 | 1604 | DataGridRow row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
|
1612 | 1605 | if (row == null)
|
1613 | 1606 | {
|
1614 |
| - targetGrid.UpdateLayout(); |
1615 |
| - if (index < targetGrid.Items.Count) |
1616 |
| - { |
1617 |
| - // scroll selected into view |
1618 |
| - targetGrid.ScrollIntoView(targetGrid.Items[index]); |
1619 |
| - row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index); |
1620 |
| - } |
1621 |
| - else |
| 1607 | + targetGrid.ScrollIntoView(targetGrid.Items[index]); |
| 1608 | + // Defer the focus once row is generated |
| 1609 | + targetGrid.Dispatcher.InvokeAsync(() => |
1622 | 1610 | {
|
1623 |
| - Console.WriteLine("selected row out of bounds: " + index); |
1624 |
| - } |
| 1611 | + var newRow = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index); |
| 1612 | + if (newRow != null) |
| 1613 | + { |
| 1614 | + newRow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); |
| 1615 | + newRow.Focus(); |
| 1616 | + Keyboard.Focus(newRow); |
| 1617 | + } |
| 1618 | + }, DispatcherPriority.Background); |
1625 | 1619 | }
|
1626 |
| - // NOTE does this causes move below? |
1627 |
| - //row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); |
1628 |
| - if (row != null) |
| 1620 | + else |
1629 | 1621 | {
|
1630 |
| - row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); // works better than Up |
| 1622 | + row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); |
1631 | 1623 | row.Focus();
|
1632 | 1624 | Keyboard.Focus(row);
|
1633 | 1625 | }
|
1634 | 1626 | }
|
1635 | 1627 |
|
| 1628 | + |
1636 | 1629 | public static string BrowseForOutputFolder(string title, string initialDirectory = null)
|
1637 | 1630 | {
|
1638 | 1631 | // https://stackoverflow.com/a/50261723/5452781
|
|
0 commit comments