andrew4582

WPF DataGrid Context Menus

Feb 2nd, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using Microsoft.Practices.Prism.Commands;
  10.  
  11. namespace Client.Models
  12. {
  13.     /// <summary>
  14.     /// Provides global DatgGrid context menu functions
  15.     /// </summary>
  16.     public class DataGridContextMenus
  17.     {
  18.         static readonly DataGridContextMenus _instance = new DataGridContextMenus();
  19.         public static DataGridContextMenus Instance
  20.         {
  21.             get { return _instance; }
  22.         }
  23.  
  24.         /// <summary>Called by the price grid's context menu</summary>
  25.         public ICommand ExportGridToExcelCommand { get; set; }
  26.  
  27.         /// <summary>Called by the grid's context menu</summary>
  28.         public ICommand CopyGridToClipboardCommand { get; set; }
  29.  
  30.         public DataGridContextMenus()
  31.         {
  32.             ExportGridToExcelCommand = new DelegateCommand<object>(OnExportGridToExcel);
  33.             CopyGridToClipboardCommand = new DelegateCommand<object>(OnCopyToClipboard);
  34.         }
  35.  
  36.         void OnExportGridToExcel(object sender)
  37.         {
  38.             var cm = sender as ContextMenu;
  39.             if (cm == null)
  40.                 return;
  41.             var grid = cm.PlacementTarget as DataGrid;
  42.             if (grid == null)
  43.                 return;
  44.  
  45.             var result = CopyGridToClipboard(grid);
  46.  
  47.             string tempfile = Path.Combine(Path.GetTempPath(), string.Format("Exported_{0}.csv", DateTime.Now.Ticks));
  48.             using (StreamWriter sw = new StreamWriter(tempfile))
  49.             {
  50.                 sw.Write(result);
  51.                 sw.Close();
  52.                 Process.Start(new ProcessStartInfo(tempfile) { UseShellExecute = true });
  53.             }
  54.         }
  55.        
  56.         void OnCopyToClipboard(object sender)
  57.         {
  58.             var cm = sender as ContextMenu;
  59.             if (cm == null)
  60.                 return;
  61.  
  62.             var grid = cm.PlacementTarget as DataGrid;
  63.             if (grid == null)
  64.                 return;
  65.             CopyGridToClipboard(grid);
  66.         }
  67.  
  68.         string CopyGridToClipboard(DataGrid grid)
  69.         {
  70.             DataGridSelectionMode selectionMode = grid.SelectionMode;
  71.             try
  72.             {
  73.                 if (selectionMode != DataGridSelectionMode.Extended)
  74.                     grid.SelectionMode = DataGridSelectionMode.Extended;
  75.                 grid.SelectAllCells();
  76.                 grid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
  77.                 ApplicationCommands.Copy.Execute(null, grid);
  78.             }
  79.             finally
  80.             {
  81.                 grid.UnselectAllCells();
  82.                 if (selectionMode != DataGridSelectionMode.Extended)
  83.                     grid.SelectionMode = selectionMode;
  84.             }
  85.  
  86.             string result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
  87.             return result;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment