|
1 | | -# How to copy a column and paste it as a new column in wpf treegrid? |
2 | | -This sample illustrates how to copy a column and paste it as a new column in wpf treegrid |
| 1 | +# How to copy a column and paste it as a new column in wpf treegrid |
| 2 | + |
| 3 | +This sample illustrates how to copy a column and paste it as a new column in [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid). |
| 4 | + |
| 5 | +You can copy a column and paste it into a new position using the context menu option in `TreeGrid`. |
| 6 | + |
| 7 | +### XAML |
| 8 | + |
| 9 | +``` xml |
| 10 | +<syncfusion:SfTreeGrid.HeaderContextMenu> |
| 11 | + <ContextMenu ItemsSource="{Binding Menu,Source={StaticResource viewmodel}}" > |
| 12 | + <ContextMenu.ItemContainerStyle> |
| 13 | + <Style TargetType="MenuItem"> |
| 14 | + <Setter Property="Command" Value="{Binding MyCommand,Source={StaticResource viewmodel}}"></Setter> |
| 15 | + <Setter Property="CommandParameter" > |
| 16 | + <Setter.Value> |
| 17 | + <MultiBinding Converter="{StaticResource ResourceKey=converter}"> |
| 18 | + <Binding RelativeSource="{RelativeSource Self}"/> |
| 19 | + <Binding /> |
| 20 | + </MultiBinding> |
| 21 | + </Setter.Value> |
| 22 | + </Setter> |
| 23 | + </Style> |
| 24 | + </ContextMenu.ItemContainerStyle> |
| 25 | + </ContextMenu> |
| 26 | + </syncfusion:SfTreeGrid.HeaderContextMenu> |
| 27 | +``` |
| 28 | + |
| 29 | +### C# |
| 30 | + |
| 31 | +``` c# |
| 32 | +private static void OnCopyColumn(object obj) |
| 33 | +{ |
| 34 | + if (obj is TreeGridColumnContextMenuInfo) |
| 35 | + { |
| 36 | + // The selected column is stored into CopiedColumn. |
| 37 | + CopiedColumn = (obj as TreeGridColumnContextMenuInfo).Column; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +private static void OnPasteColumn(object obj) |
| 42 | +{ |
| 43 | + if (obj is TreeGridColumnContextMenuInfo && CopiedColumn != null) |
| 44 | + { |
| 45 | + var grid = (obj as TreeGridColumnContextMenuInfo).TreeGrid; |
| 46 | + // Get the index for corresponding column. |
| 47 | + var index = grid.Columns.IndexOf((obj as TreeGridColumnContextMenuInfo).Column); |
| 48 | + // Copy the column and insert based on the index position. |
| 49 | + grid.Columns.Insert(index + 1, new TreeGridTextColumn() { MappingName = CopiedColumn.MappingName }); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
0 commit comments