WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴。WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用DataGridView控件。如果想进行DataGrid的复制粘贴,只需要在进行复制粘贴的时候,将剪切板上的数据替换成绑定的数据,同样,插入删除等操作,都是改变绑定数据。如下,是一个粘贴的方法,将剪切板的数据转化为绑定数据。

 

[csharp] view plain copy
 
  1. #region ctrl+c粘贴  
  2.         private void DataGirdViewCellPaste()  
  3.         {  
  4.             try  
  5.             {  
  6.                 // 获取剪切板的内容,并按行分割     
  7.                 string pasteText = Clipboard.GetText();  
  8.                 if (string.IsNullOrEmpty(pasteText))  
  9.                     return;  
  10.                 int tnum = 0;//剪贴板列数  
  11.                 int nnum = 0;//剪贴板行数  
  12.                 //获得当前剪贴板内容的行、列数  
  13.                 for (int i = 0; i < pasteText.Length; i++)  
  14.                 {  
  15.                     if (pasteText.Substring(i, 1) == "\t")  
  16.                     {  
  17.                         tnum++;  
  18.                     }  
  19.                     if (pasteText.Substring(i, 1) == "\n")  
  20.                     {  
  21.                         nnum++;  
  22.                     }  
  23.                 }  
  24.                 Object[,] data;  
  25.                 //粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\n  
  26.                 if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")  
  27.                 {  
  28.                     nnum = nnum - 1;  
  29.                 }  
  30.                 tnum = tnum / (nnum + 1);  
  31.                 data = new object[nnum + 1, tnum + 1];//定义一个二维数组  
  32.                 String rowstr;  
  33.                 rowstr = "";  
  34.                 //MessageBox.Show(pasteText.IndexOf("B").ToString());  
  35.                 //对数组赋值  
  36.                 for (int i = 0; i < (nnum + 1); i++)  
  37.                 {  
  38.                     for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)  
  39.                     {  
  40.                         //一行中的最后一列  
  41.                         if (colIndex == tnum && pasteText.IndexOf("\r") != -1)  
  42.                         {  
  43.                             rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));  
  44.                         }  
  45.                         //最后一行的最后一列  
  46.                         if (colIndex == tnum && pasteText.IndexOf("\r") == -1)  
  47.                         {  
  48.                             rowstr = pasteText.Substring(0);  
  49.                         }  
  50.                         //其他行列  
  51.                         if (colIndex != tnum)  
  52.                         {  
  53.                             rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));  
  54.                             pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);  
  55.                         }  
  56.                         data[i, colIndex] = rowstr;  
  57.                     }  
  58.                     //截取下一行数据  
  59.                     pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);  
  60.                 }  
  61.                 //获取获取当前选中单元格所在的行序号  
  62.                 int rowindex = dataGrid.SelectedIndex;  
  63.   
  64.                 List<BoxGriderModel> listBoxGriderModel = new List<BoxGriderModel>();  
  65.                 for (int j = 0; j < (nnum + 1); j++)  
  66.                 {  
  67.                     listBoxGriderModel.Add(new BoxGriderModel(double.Parse(data[j, 0].ToString()), double.Parse(data[j, 1].ToString()), double.Parse(data[j, 2].ToString()), double.Parse(data[j, 3].ToString()),  
  68.                         double.Parse(data[j, 4].ToString()), double.Parse(data[j, 5].ToString()), double.Parse(data[j, 6].ToString())));  
  69.   
  70.                 }  
  71.                 m_model.ListBoxGriderModel = listBoxGriderModel;  
  72.                 this.dataGrid.ItemsSource = m_model.ListBoxGriderModel;  
  73.             }  
  74.             catch  
  75.             {  
  76.                 MessageBox.Show("粘贴区域大小不一致");  
  77.                 return;  
  78.             }  
  79.         }  
  80.         #endregion