Skip to content

Commit

Permalink
Drop from 3 clicks to 1 to edit transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Oct 7, 2021
1 parent e7633ca commit a1b4ebf
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/MoneyMan.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace MoneyMan
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using Squirrel;

/// <summary>
Expand Down Expand Up @@ -49,5 +50,15 @@ internal static UpdateManager CreateUpdateManager()
urlOrPath: $"https://moneymanreleases.blob.core.windows.net/releases/{channel}/{subchannel}/",
applicationName);
}

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

EventManager.RegisterClassHandler(
typeof(DataGrid),
DataGrid.PreviewMouseLeftButtonDownEvent,
new RoutedEventHandler(WpfHelpers.DataGridPreviewMouseLeftButtonDownEvent));
}
}
}
1 change: 1 addition & 0 deletions src/MoneyMan.WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<DataGrid Name="TransactionDataGrid"
AlternatingRowBackground="LightBlue"
AlternationCount="2"
PreparingCellForEdit="TransactionDataGrid_PreparingCellForEdit"
ItemsSource="{Binding Source={StaticResource transactions}}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding Document.SelectedTransaction, Source={StaticResource viewModel}, Mode=TwoWay, TargetNullValue={x:Static CollectionView.NewItemPlaceholder}}"
Expand Down
5 changes: 5 additions & 0 deletions src/MoneyMan.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,10 @@ private async Task UpdateApplicationAsync()
// This is newer than the latest stable version.
}
}

private void TransactionDataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
WpfHelpers.GetFirstChildByType<Control>(e.EditingElement)?.Focus();
}
}
}
80 changes: 80 additions & 0 deletions src/MoneyMan.WPF/WpfHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the Ms-PL license. See LICENSE.txt file in the project root for full license information.

namespace MoneyMan
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

public static class WpfHelpers
{
internal static void DataGridPreviewMouseLeftButtonDownEvent(object sender, RoutedEventArgs e)
{
// The original source for this was inspired by https://softwaremechanik.wordpress.com/2013/10/02/how-to-make-all-wpf-datagrid-cells-have-a-single-click-to-edit/
DataGridCell? cell = e is MouseButtonEventArgs { OriginalSource: UIElement clickTarget } ? FindVisualParent<DataGridCell>(clickTarget) : null;
if (cell is { IsEditing: false, IsReadOnly: false })
{
if (!cell.IsFocused)
{
cell.Focus();
}

if (FindVisualParent<DataGrid>(cell) is DataGrid dataGrid)
{
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected)
{
cell.IsSelected = true;
}
}
else
{
if (FindVisualParent<DataGridRow>(cell) is DataGridRow { IsSelected: false } row)
{
row.IsSelected = true;
}
}
}
}
}

internal static T? GetFirstChildByType<T>(DependencyObject prop)
where T : DependencyObject
{
int count = VisualTreeHelper.GetChildrenCount(prop);
for (int i = 0; i < count; i++)
{
if (VisualTreeHelper.GetChild(prop, i) is DependencyObject child)
{
T? typedChild = child as T ?? GetFirstChildByType<T>(child);
if (typedChild is object)
{
return typedChild;
}
}
}

return null;
}

private static T? FindVisualParent<T>(UIElement element)
where T : UIElement
{
UIElement? parent = element;
while (parent is object)
{
if (parent is T correctlyTyped)
{
return correctlyTyped;
}

parent = VisualTreeHelper.GetParent(parent) as UIElement;
}

return null;
}
}
}

0 comments on commit a1b4ebf

Please sign in to comment.