Skip to content

Commit a12deb2

Browse files
committed
Add support for ctrl+v in data grids
1 parent 8ce67a1 commit a12deb2

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

solution/GraphicalDebugging/GeometryWatchControl.xaml.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,20 @@ private void GeometryWatchWindow_SizeChanged(object sender, SizeChangedEventArgs
150150

151151
private void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
152152
{
153+
if (m_isDataGridEdited)
154+
return;
155+
153156
if (e.Key == System.Windows.Input.Key.Delete)
154157
{
155-
if (m_isDataGridEdited)
156-
return;
157-
158158
Util.RemoveDataGridItems(dataGrid, Geometries,
159159
(int selectIndex) => ResetAt(new GeometryItem(), selectIndex),
160160
(GeometryItem geometry) => m_colorIds.Push(geometry.ColorId),
161161
() => UpdateItems(false));
162162
}
163+
else if (e.Key == System.Windows.Input.Key.V && e.KeyboardDevice.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
164+
{
165+
Util.PasteDataGridItemFromClipboard(dataGrid, Geometries);
166+
}
163167
}
164168

165169
private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)

solution/GraphicalDebugging/GraphicalWatchControl.xaml.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,20 @@ private void ExpressionLoader_BreakModeEntered()
7676

7777
private void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
7878
{
79+
if (m_isDataGridEdited)
80+
return;
81+
7982
if (e.Key == System.Windows.Input.Key.Delete)
8083
{
81-
if (m_isDataGridEdited)
82-
return;
83-
8484
Util.RemoveDataGridItems(dataGrid, Variables,
8585
(int selectIndex) => ResetAt(new GraphicalItem(), selectIndex),
8686
(GraphicalItem variable) => { },
8787
() => { });
8888
}
89+
else if (e.Key == System.Windows.Input.Key.V && e.KeyboardDevice.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
90+
{
91+
Util.PasteDataGridItemFromClipboard(dataGrid, Variables);
92+
}
8993
}
9094

9195
private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)

solution/GraphicalDebugging/PlotWatchControl.xaml.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,20 @@ private void PlotWatchWindow_SizeChanged(object sender, SizeChangedEventArgs e)
151151

152152
private void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
153153
{
154+
if (m_isDataGridEdited)
155+
return;
156+
154157
if (e.Key == System.Windows.Input.Key.Delete)
155158
{
156-
if (m_isDataGridEdited)
157-
return;
158-
159159
Util.RemoveDataGridItems(dataGrid, Plots,
160160
(int selectIndex) => ResetAt(new PlotItem(), selectIndex),
161161
(PlotItem plot) => m_colorIds.Push(plot.ColorId),
162162
() => UpdateItems(false));
163163
}
164+
else if (e.Key == System.Windows.Input.Key.V && e.KeyboardDevice.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
165+
{
166+
Util.PasteDataGridItemFromClipboard(dataGrid, Plots);
167+
}
164168
}
165169

166170
private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)

solution/GraphicalDebugging/Util.cs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,23 @@ public static void SortDsc<T>(T[] array)
324324
});
325325
}
326326

327+
public static int[] DataGridSelectedIndexes(System.Windows.Controls.DataGrid dataGrid)
328+
{
329+
int[] indexes = new int[dataGrid.SelectedItems.Count];
330+
int i = 0;
331+
foreach (var item in dataGrid.SelectedItems)
332+
{
333+
indexes[i] = dataGrid.Items.IndexOf(item);
334+
++i;
335+
}
336+
return indexes;
337+
}
338+
327339
public delegate void ItemUpdatedPredicate(int index);
328340
public delegate void ItemInsertEmptyPredicate(int index);
329341
public delegate void ItemRemovePredicate<Item>(Item item);
330342
public delegate void ItemRemovedPredicate(int index);
331-
public delegate void ItemsRemovedPredicate();
343+
public delegate void ItemsRemovedPredicate();
332344

333345
public static void RemoveDataGridItems<Item>(System.Windows.Controls.DataGrid dataGrid,
334346
System.Collections.ObjectModel.ObservableCollection<Item> itemsCollection,
@@ -339,13 +351,7 @@ public static void RemoveDataGridItems<Item>(System.Windows.Controls.DataGrid da
339351
if (dataGrid.SelectedItems.Count < 1)
340352
return;
341353

342-
int[] indexes = new int[dataGrid.SelectedItems.Count];
343-
int i = 0;
344-
foreach (var item in dataGrid.SelectedItems)
345-
{
346-
indexes[i] = dataGrid.Items.IndexOf(item);
347-
++i;
348-
}
354+
int[] indexes = DataGridSelectedIndexes(dataGrid);
349355

350356
Util.SortDsc(indexes);
351357

@@ -474,13 +480,7 @@ public static void EnableDataGridItems<Item>(System.Windows.Controls.DataGrid da
474480
if (dataGrid.SelectedItems.Count < 1)
475481
return;
476482

477-
int[] indexes = new int[dataGrid.SelectedItems.Count];
478-
int i = 0;
479-
foreach (var item in dataGrid.SelectedItems)
480-
{
481-
indexes[i] = dataGrid.Items.IndexOf(item);
482-
++i;
483-
}
483+
int[] indexes = DataGridSelectedIndexes(dataGrid);
484484

485485
foreach (int index in indexes)
486486
{
@@ -489,6 +489,25 @@ public static void EnableDataGridItems<Item>(System.Windows.Controls.DataGrid da
489489
}
490490
}
491491

492+
public static void PasteDataGridItemFromClipboard<Item>(System.Windows.Controls.DataGrid dataGrid,
493+
System.Collections.ObjectModel.ObservableCollection<Item> itemsCollection)
494+
where Item : VariableItem
495+
{
496+
string text = Clipboard.GetText();
497+
if (string.IsNullOrEmpty(text))
498+
return;
499+
500+
if (dataGrid.SelectedItems.Count != 1)
501+
return;
502+
503+
int index = dataGrid.Items.IndexOf(dataGrid.SelectedItems[0]);
504+
if (index < 0 || index >= dataGrid.Items.Count)
505+
return;
506+
507+
Item item = itemsCollection[index];
508+
item.Name = text;
509+
}
510+
492511
// https://softwaremechanik.wordpress.com/2013/10/02/how-to-make-all-wpf-datagrid-cells-have-a-single-click-to-edit/
493512
public static void DataGridSingleClickHack(DependencyObject originalSource)
494513
{

solution/GraphicalDebugging/release_notes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ bugfixes:
66

77
additions:
88
- expression error in Type column
9-
- context menus for variables
9+
- context menus for row of list of variables
10+
- support for ctrl+v for row of list of variables
1011

1112
version 0.52
1213

0 commit comments

Comments
 (0)