Skip to content

Commit 306250e

Browse files
committed
Single click edit.
1 parent fa11f8d commit 306250e

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

Gu.Wpf.DataGrid2D.Demo/Gu.Wpf.DataGrid2D.Demo.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@
111111
<Compile Include="Array2DView.xaml.cs">
112112
<DependentUpon>Array2DView.xaml</DependentUpon>
113113
</Compile>
114+
<Compile Include="SingleClickEditItem.cs">
115+
<DependentUpon>SingleClickEditView.xaml</DependentUpon>
116+
</Compile>
117+
<Compile Include="SingleClickEditView.xaml.cs">
118+
<DependentUpon>SingleClickEditView.xaml</DependentUpon>
119+
</Compile>
120+
<Compile Include="SingleClickEditViewModel.cs">
121+
<DependentUpon>SingleClickEditView.xaml</DependentUpon>
122+
</Compile>
114123
<Compile Include="TransposedView.xaml.cs">
115124
<DependentUpon>TransposedView.xaml</DependentUpon>
116125
</Compile>
@@ -166,6 +175,10 @@
166175
<SubType>Designer</SubType>
167176
<Generator>MSBuild:Compile</Generator>
168177
</Page>
178+
<Page Include="SingleClickEditView.xaml">
179+
<SubType>Designer</SubType>
180+
<Generator>MSBuild:Compile</Generator>
181+
</Page>
169182
<Page Include="TransposedView.xaml">
170183
<SubType>Designer</SubType>
171184
<Generator>MSBuild:Compile</Generator>

Gu.Wpf.DataGrid2D.Demo/MainWindow.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@
4141
<TabItem AutomationProperties.AutomationId="{x:Static demo:AutomationIds.CellTemplateTab}" Header="CellTemplate">
4242
<demo:CellTemplateView />
4343
</TabItem>
44+
45+
<TabItem Header="Single click edit.">
46+
<demo:SingleClickEditView />
47+
</TabItem>
4448
</TabControl>
4549
</Window>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace Gu.Wpf.DataGrid2D.Demo
2+
{
3+
using System.ComponentModel;
4+
using System.Runtime.CompilerServices;
5+
using JetBrains.Annotations;
6+
7+
public class SingleClickEditItem : INotifyPropertyChanged
8+
{
9+
private string stringValue;
10+
private int intValue;
11+
private bool boolValue;
12+
13+
public event PropertyChangedEventHandler PropertyChanged;
14+
15+
public string StringValue
16+
{
17+
get
18+
{
19+
return this.stringValue;
20+
}
21+
22+
set
23+
{
24+
if (value == this.stringValue)
25+
{
26+
return;
27+
}
28+
this.stringValue = value;
29+
this.OnPropertyChanged();
30+
}
31+
}
32+
33+
public int IntValue
34+
{
35+
get
36+
{
37+
return this.intValue;
38+
}
39+
40+
set
41+
{
42+
if (value == this.intValue)
43+
{
44+
return;
45+
}
46+
this.intValue = value;
47+
this.OnPropertyChanged();
48+
}
49+
}
50+
51+
public bool BoolValue
52+
{
53+
get
54+
{
55+
return this.boolValue;
56+
}
57+
58+
set
59+
{
60+
if (value == this.boolValue)
61+
{
62+
return;
63+
}
64+
this.boolValue = value;
65+
this.OnPropertyChanged();
66+
}
67+
}
68+
69+
[NotifyPropertyChangedInvocator]
70+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
71+
{
72+
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
73+
}
74+
}
75+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<UserControl x:Class="Gu.Wpf.DataGrid2D.Demo.SingleClickEditView"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:dataGrid2D="http://gu.se/DataGrid2D"
6+
xmlns:local="clr-namespace:Gu.Wpf.DataGrid2D.Demo"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
d:DesignHeight="300"
9+
d:DesignWidth="300"
10+
mc:Ignorable="d">
11+
<UserControl.DataContext>
12+
<local:SingleClickEditViewModel />
13+
</UserControl.DataContext>
14+
<Grid>
15+
<DataGrid dataGrid2D:SingleClick.Edit="True" ItemsSource="{Binding Items}" SelectionUnit="FullRow" />
16+
</Grid>
17+
</UserControl>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Gu.Wpf.DataGrid2D.Demo
2+
{
3+
using System.Windows.Controls;
4+
5+
/// <summary>
6+
/// Interaction logic for SingleClickEditView.xaml
7+
/// </summary>
8+
public partial class SingleClickEditView : UserControl
9+
{
10+
public SingleClickEditView()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Gu.Wpf.DataGrid2D.Demo
2+
{
3+
using System.Collections.ObjectModel;
4+
5+
public class SingleClickEditViewModel
6+
{
7+
public SingleClickEditViewModel()
8+
{
9+
this.Items = new ObservableCollection<SingleClickEditItem>
10+
{
11+
new SingleClickEditItem(),
12+
new SingleClickEditItem(),
13+
};
14+
}
15+
16+
public ObservableCollection<SingleClickEditItem> Items { get; }
17+
}
18+
}

Gu.Wpf.DataGrid2D/Gu.Wpf.DataGrid2D.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="ItemsSource.Array2D.cs" />
6767
<Compile Include="RowColumnIndex.cs" />
6868
<Compile Include="RowColumnIndexConverter.cs" />
69+
<Compile Include="SingleClick.cs" />
6970
<Compile Include="Views\Array2DRowView.cs" />
7071
<Compile Include="Views\Array2DView.cs" />
7172
<Compile Include="Views\Array2DIndexPropertyDescriptor.cs" />

Gu.Wpf.DataGrid2D/SingleClick.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace Gu.Wpf.DataGrid2D
2+
{
3+
using System.Linq;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
7+
public static class SingleClick
8+
{
9+
public static readonly DependencyProperty EditProperty = DependencyProperty.RegisterAttached(
10+
"Edit",
11+
typeof(bool),
12+
typeof(SingleClick),
13+
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
14+
15+
static SingleClick()
16+
{
17+
EventManager.RegisterClassHandler(typeof(DataGridCell), UIElement.PreviewMouseLeftButtonDownEvent, new RoutedEventHandler(OnPreviewMouseLeftButtonDown));
18+
}
19+
20+
public static void SetEdit(DependencyObject element, bool value)
21+
{
22+
element.SetValue(EditProperty, value);
23+
}
24+
25+
public static bool GetEdit(DependencyObject element)
26+
{
27+
return (bool)element.GetValue(EditProperty);
28+
}
29+
30+
private static void OnPreviewMouseLeftButtonDown(object sender, RoutedEventArgs e)
31+
{
32+
var cell = (DataGridCell)sender;
33+
if (GetEdit(cell) && !cell.IsReadOnly && !cell.IsEditing)
34+
{
35+
var dataGrid = cell.Ancestors()
36+
.OfType<DataGrid>()
37+
.FirstOrDefault();
38+
if (dataGrid == null)
39+
{
40+
return;
41+
}
42+
43+
dataGrid.BeginEdit();
44+
if (dataGrid.SelectionUnit == DataGridSelectionUnit.FullRow)
45+
{
46+
cell.Ancestors()
47+
.OfType<DataGridRow>()
48+
.FirstOrDefault()
49+
?.SetCurrentValue(DataGridRow.IsSelectedProperty, true);
50+
}
51+
else
52+
{
53+
cell.SetCurrentValue(DataGridCell.IsSelectedProperty, true);
54+
}
55+
56+
cell.Focus();
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)