Skip to content

Commit 23023d7

Browse files
authored
Adding spacebar handler to make editing combobox column easier. (#1516)
Addresses part of #1284
1 parent bd09ca5 commit 23023d7

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

MainDemo.Wpf/Grids.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="MaterialDesignColors.WpfExample.Grids"
1+
<UserControl x:Class="MaterialDesignColors.WpfExample.Grids"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -46,7 +46,7 @@
4646
Header="Description"
4747
MaxLength="255"
4848
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}" />
49-
<materialDesign:DataGridTextColumn Binding="{Binding Numeric}"
49+
<materialDesign:DataGridTextColumn Binding="{Binding Numeric}"
5050
Header="Number with long header"
5151
Width="120"
5252
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}">
@@ -71,8 +71,8 @@
7171

7272
<!-- use custom combo box column to get better combos. Use ItemsSourceBinding as your binding template to be applied to each combo -->
7373
<materialDesign:DataGridComboBoxColumn Header="Food" IsEditable="True"
74-
SelectedValueBinding="{Binding Food}"
75-
ItemsSourceBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Foods}">
74+
SelectedValueBinding="{Binding Food}"
75+
ItemsSourceBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Foods}">
7676
<!--Setting the editing element style allows access to all of the combo box's properties
7777
<materialDesign:MaterialDataGridComboBoxColumn.EditingElementStyle>
7878
<Style TargetType="ComboBox" BasedOn="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type ComboBox}, ResourceId=MaterialDataGridComboBoxColumnEditingStyle}}" >

MaterialDesignThemes.Wpf/DataGridAssist.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,27 @@ private static void EnableCheckBoxAssistPropertyChangedCallback(DependencyObject
144144
if (dataGrid == null) return;
145145

146146
if ((bool) dependencyPropertyChangedEventArgs.NewValue)
147+
{
147148
dataGrid.PreviewMouseLeftButtonDown += DataGridOnPreviewMouseLeftButtonDown;
149+
dataGrid.KeyDown += DataGridOnKeyDown;
150+
}
148151
else
152+
{
149153
dataGrid.PreviewMouseLeftButtonDown -= DataGridOnPreviewMouseLeftButtonDown;
154+
dataGrid.KeyDown -= DataGridOnKeyDown;
155+
}
156+
}
157+
158+
private static void DataGridOnKeyDown(object sender, KeyEventArgs e)
159+
{
160+
if (e.Key == Key.Space &&
161+
e.OriginalSource is DataGridCell cell &&
162+
cell.IsReadOnly == false &&
163+
cell.Column is DataGridComboBoxColumn &&
164+
sender is DataGrid dataGrid)
165+
{
166+
dataGrid.BeginEdit();
167+
}
150168
}
151169

152170
private static void DataGridOnPreviewMouseLeftButtonDown(object sender,
@@ -159,7 +177,7 @@ private static void DataGridOnPreviewMouseLeftButtonDown(object sender,
159177

160178
while (inputHitTest != null)
161179
{
162-
var dataGridCell = inputHitTest as DataGridCell;
180+
var dataGridCell = inputHitTest as DataGridCell;
163181
if (dataGridCell != null && dataGrid.Equals(dataGridCell.GetVisualAncestry().OfType<DataGrid>().FirstOrDefault()))
164182
{
165183
if (dataGridCell.IsReadOnly) return;

0 commit comments

Comments
 (0)