Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Gu.Wpf.DataGrid2D.Demo/Gu.Wpf.DataGrid2D.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@
<Compile Include="Array2DView.xaml.cs">
<DependentUpon>Array2DView.xaml</DependentUpon>
</Compile>
<Compile Include="SingleClickEditItem.cs">
<DependentUpon>SingleClickEditView.xaml</DependentUpon>
</Compile>
<Compile Include="SingleClickEditView.xaml.cs">
<DependentUpon>SingleClickEditView.xaml</DependentUpon>
</Compile>
<Compile Include="SingleClickEditViewModel.cs">
<DependentUpon>SingleClickEditView.xaml</DependentUpon>
</Compile>
<Compile Include="TransposedView.xaml.cs">
<DependentUpon>TransposedView.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -166,6 +175,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="SingleClickEditView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="TransposedView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
4 changes: 4 additions & 0 deletions Gu.Wpf.DataGrid2D.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@
<TabItem AutomationProperties.AutomationId="{x:Static demo:AutomationIds.CellTemplateTab}" Header="CellTemplate">
<demo:CellTemplateView />
</TabItem>

<TabItem Header="Single click edit.">
<demo:SingleClickEditView />
</TabItem>
</TabControl>
</Window>
75 changes: 75 additions & 0 deletions Gu.Wpf.DataGrid2D.Demo/SingleClickEditItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace Gu.Wpf.DataGrid2D.Demo
{
using System.ComponentModel;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;

public class SingleClickEditItem : INotifyPropertyChanged
{
private string stringValue;
private int intValue;
private bool boolValue;

public event PropertyChangedEventHandler PropertyChanged;

public string StringValue
{
get
{
return this.stringValue;
}

set
{
if (value == this.stringValue)
{
return;
}
this.stringValue = value;
this.OnPropertyChanged();
}
}

public int IntValue
{
get
{
return this.intValue;
}

set
{
if (value == this.intValue)
{
return;
}
this.intValue = value;
this.OnPropertyChanged();
}
}

public bool BoolValue
{
get
{
return this.boolValue;
}

set
{
if (value == this.boolValue)
{
return;
}
this.boolValue = value;
this.OnPropertyChanged();
}
}

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
17 changes: 17 additions & 0 deletions Gu.Wpf.DataGrid2D.Demo/SingleClickEditView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<UserControl x:Class="Gu.Wpf.DataGrid2D.Demo.SingleClickEditView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dataGrid2D="http://gu.se/DataGrid2D"
xmlns:local="clr-namespace:Gu.Wpf.DataGrid2D.Demo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<UserControl.DataContext>
<local:SingleClickEditViewModel />
</UserControl.DataContext>
<Grid>
<DataGrid dataGrid2D:SingleClick.Edit="True" ItemsSource="{Binding Items}" SelectionUnit="FullRow" />
</Grid>
</UserControl>
15 changes: 15 additions & 0 deletions Gu.Wpf.DataGrid2D.Demo/SingleClickEditView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Gu.Wpf.DataGrid2D.Demo
{
using System.Windows.Controls;

/// <summary>
/// Interaction logic for SingleClickEditView.xaml
/// </summary>
public partial class SingleClickEditView : UserControl
{
public SingleClickEditView()
{
InitializeComponent();
}
}
}
18 changes: 18 additions & 0 deletions Gu.Wpf.DataGrid2D.Demo/SingleClickEditViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Gu.Wpf.DataGrid2D.Demo
{
using System.Collections.ObjectModel;

public class SingleClickEditViewModel
{
public SingleClickEditViewModel()
{
this.Items = new ObservableCollection<SingleClickEditItem>
{
new SingleClickEditItem(),
new SingleClickEditItem(),
};
}

public ObservableCollection<SingleClickEditItem> Items { get; }
}
}
1 change: 1 addition & 0 deletions Gu.Wpf.DataGrid2D/Gu.Wpf.DataGrid2D.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="ItemsSource.Array2D.cs" />
<Compile Include="RowColumnIndex.cs" />
<Compile Include="RowColumnIndexConverter.cs" />
<Compile Include="SingleClick.cs" />
<Compile Include="Views\Array2DRowView.cs" />
<Compile Include="Views\Array2DView.cs" />
<Compile Include="Views\Array2DIndexPropertyDescriptor.cs" />
Expand Down
60 changes: 60 additions & 0 deletions Gu.Wpf.DataGrid2D/SingleClick.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace Gu.Wpf.DataGrid2D
{
using System.Linq;
using System.Windows;
using System.Windows.Controls;

public static class SingleClick
{
public static readonly DependencyProperty EditProperty = DependencyProperty.RegisterAttached(
"Edit",
typeof(bool),
typeof(SingleClick),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));

static SingleClick()
{
EventManager.RegisterClassHandler(typeof(DataGridCell), UIElement.PreviewMouseLeftButtonDownEvent, new RoutedEventHandler(OnPreviewMouseLeftButtonDown));
}

public static void SetEdit(DependencyObject element, bool value)
{
element.SetValue(EditProperty, value);
}

public static bool GetEdit(DependencyObject element)
{
return (bool)element.GetValue(EditProperty);
}

private static void OnPreviewMouseLeftButtonDown(object sender, RoutedEventArgs e)
{
var cell = (DataGridCell)sender;
if (GetEdit(cell) && !cell.IsReadOnly && !cell.IsEditing)
{
var dataGrid = cell.Ancestors()
.OfType<DataGrid>()
.FirstOrDefault();
if (dataGrid == null)
{
return;
}

dataGrid.BeginEdit();
if (dataGrid.SelectionUnit == DataGridSelectionUnit.FullRow)
{
cell.Ancestors()
.OfType<DataGridRow>()
.FirstOrDefault()
?.SetCurrentValue(DataGridRow.IsSelectedProperty, true);
}
else
{
cell.SetCurrentValue(DataGridCell.IsSelectedProperty, true);
}

cell.Focus();
}
}
}
}