Skip to content

Commit ca80479

Browse files
committed
Fix #419: Adding a new key should focus the new item.
1 parent f31ebf8 commit ca80479

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/ResXManager.View/Tools/AddNewKeyCommand.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Composition;
55
using System.Linq;
66
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Threading;
79

810
using ResXManager.View.Properties;
911
using ResXManager.View.Visuals;
@@ -12,7 +14,7 @@
1214
using TomsToolbox.Wpf;
1315

1416
[Export, Shared]
15-
internal class AddNewKeyCommand : DelegateCommand<DependencyObject>
17+
internal class AddNewKeyCommand : DelegateCommand<DataGrid>
1618
{
1719
private readonly ResourceViewModel _resourceViewModel;
1820
private readonly IExportProvider _exportProvider;
@@ -26,7 +28,7 @@ public AddNewKeyCommand(ResourceViewModel resourceViewModel, IExportProvider exp
2628
ExecuteCallback = InternalExecute;
2729
}
2830

29-
private void InternalExecute(DependencyObject? parameter)
31+
private void InternalExecute(DataGrid? dataGrid)
3032
{
3133
if (_resourceViewModel.SelectedEntities.Count != 1)
3234
{
@@ -47,7 +49,7 @@ private void InternalExecute(DependencyObject? parameter)
4749

4850
var application = Application.Current;
4951

50-
var owner = parameter != null ? Window.GetWindow(parameter) : application.MainWindow;
52+
var owner = dataGrid != null ? Window.GetWindow(dataGrid) : application.MainWindow;
5153

5254
var inputBox = new InputBox(_exportProvider)
5355
{
@@ -76,6 +78,29 @@ private void InternalExecute(DependencyObject? parameter)
7678
try
7779
{
7880
_resourceViewModel.AddNewKey(resourceFile, key);
81+
82+
dataGrid?.BeginInvoke(() =>
83+
{
84+
var selectedItem = dataGrid.SelectedItem;
85+
dataGrid.ScrollIntoView(selectedItem);
86+
dataGrid?.BeginInvoke(DispatcherPriority.Background, () =>
87+
{
88+
var container = dataGrid.ItemContainerGenerator.ContainerFromItem(selectedItem);
89+
if (container == null)
90+
return;
91+
92+
dataGrid?.BeginInvoke(DispatcherPriority.Background, () =>
93+
{
94+
var element = container
95+
.VisualDescendantsAndSelf()
96+
.OfType<UIElement>()
97+
.FirstOrDefault(item => item.Focusable);
98+
99+
element?.Focus();
100+
});
101+
});
102+
});
103+
79104
}
80105
catch (Exception ex)
81106
{

src/ResXManager.View/Visuals/ResourceView.xaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
xmlns:themes="clr-namespace:ResXManager.View.Themes"
1919
xmlns:composition="urn:TomsToolbox.Composition"
2020
xmlns:Interactions="http://schemas.microsoft.com/xaml/behaviors"
21-
x:Name="LayoutRoot"
2221
d:DataContext="{d:DesignInstance visuals:ResourceViewModel}"
2322
Padding="4">
2423
<UserControl.Resources>
@@ -78,11 +77,6 @@
7877

7978
</UserControl.Resources>
8079

81-
<UserControl.InputBindings>
82-
<KeyBinding Key="N" Modifiers="Control" Command="{composition:Import tools:AddNewKeyCommand}" CommandParameter="{Binding ElementName=LayoutRoot}" />
83-
<KeyBinding Key="Insert" Modifiers="Shift" Command="{composition:Import tools:AddNewKeyCommand}" CommandParameter="{Binding ElementName=LayoutRoot}" />
84-
</UserControl.InputBindings>
85-
8680
<Grid FocusManager.FocusedElement="{Binding ElementName=DataGrid}">
8781

8882
<Grid.ColumnDefinitions>
@@ -283,7 +277,7 @@
283277
</Button>
284278
<Button ToolTip="{x:Static properties:Resources.AddKeyToolTip}"
285279
Style="{StaticResource ToolbarCommandButtonStyle}"
286-
Command="{composition:Import tools:AddNewKeyCommand}" CommandParameter="{Binding ElementName=LayoutRoot}">
280+
Command="{composition:Import tools:AddNewKeyCommand}" CommandParameter="{Binding ElementName=DataGrid}">
287281
<Image SnapsToDevicePixels="True" Source="/ResXManager.View;component/Assets/add.png" />
288282
</Button>
289283
<Separator />
@@ -697,6 +691,11 @@
697691

698692
</Grid>
699693

694+
<UserControl.InputBindings>
695+
<KeyBinding Key="N" Modifiers="Control" Command="{composition:Import tools:AddNewKeyCommand}" CommandParameter="{Binding ElementName=DataGrid}" />
696+
<KeyBinding Key="Insert" Modifiers="Shift" Command="{composition:Import tools:AddNewKeyCommand}" CommandParameter="{Binding ElementName=DataGrid}" />
697+
</UserControl.InputBindings>
698+
700699
<Interactions:Interaction.Behaviors>
701700
<composition:CommandRoutingBehavior CommandSource="view:DeleteCommand"
702701
CommandTarget="{Binding DeleteCommand, Converter={StaticResource DeleteCommandConverter}}"

0 commit comments

Comments
 (0)