@@ -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 {
0 commit comments