如何在两个C1DataGrid中实现拖拽项目

发布时间:2014/12/31 00:12 发布者:gw0506

返回博客中心

ComponentOne Studio for WPF 为我们提供了一个非常实用的类C1DragDropManager ,使用它我们可以轻松处理程序与用户交互时的拖拽操作。使用C1DragDropManager类我们只需进行简单的配置,配置完成后,对拖拽操作的支持即刻被添加到您的应用中。

通过使用该类,我们能开启应用中对拖拽命令的支持,对其进行定制,并允许移动及复制操作。本篇博客的目的,在于讨论在两个C1DataGrid 控件间拖拽多行数据的问题。

C1DataGrid 允许最终用户从一个该种控件中选定任意行内容轻松通过拖拽操作将内容传到另一个C1DataGrid控件。在选定及拖拽移动过程中,控件都会提供标识帮助用户确定选定区域范围和目标放置区域。

DragDrop rows from one C1DataGrid to another

 

将C1DataGrid的ItemsSource属性设置(见如下代码)过后就可以开始着手具体拖拽命令对应操作的实现了。

c1DataGrid1.ItemsSource = Person.Generate(10);
c1DataGrid2.ItemsSource = Person.Generate(10);

接下来我们要加入C1DragDropManager 控件,代码如下。

C1DragDropManager ddm = new C1DragDropManager();

向应用中成功添加C1DragDropManager 后我们要将两个C1DataGrid控件的拖动来源和放置目标进行设置。

c1DataGrid1.LoadedRowPresenter += (s, e) =>
{
  ddm.RegisterDropTarget(e.Row.Presenter, true);
  ddm.RegisterDragSource(e.Row.Presenter, DragDropEffect.Copy, ModifierKeys.None);
};
 
c1DataGrid2.LoadedRowPresenter += (s, e) =>
{
  ddm.RegisterDropTarget(e.Row.Presenter, true);
  ddm.RegisterDragSource(e.Row.Presenter, DragDropEffect.Copy, ModifierKeys.None);
};

接下来定制DragStartDragDrop 事件来完成我们所要求的操作。

void DragStart(object source, DragDropEventArgs e)
{
  C1.WPF.DataGrid.DataGridRow row = ((DataGridRowPresenter)e.DragSource).Row;
  string mark = row.DataItem.ToString();
  Border border = ddm.TargetMarker as Border;
  border.Height = 32d;
  border.Child = new ContentControl()
    {
        Foreground = new SolidColorBrush(Colors.Black),
        FontSize = 16d,
        FontWeight = FontWeights.DemiBold,
        Content = mark
    };
}
 
void DragDrop(object source, DragDropEventArgs e)
{
   var source_row = ((DataGridRowPresenter)e.DragSource).Row;
   var source_table = source_row.DataGrid;
 
   var target_row = ((DataGridRowPresenter)e.DropTarget).Row;
   var target_table = target_row.DataGrid;
 
   if (source_table != target_table)
     {
        ((ObservableCollection<Person>)source_table.ItemsSource).Remove((Person)source_row.DataItem);
        ((ObservableCollection<Person>)target_table.ItemsSource).Add((Person)source_row.DataItem);
     }
   else
     System.Media.SystemSounds.Beep.Play();
}

请在以下链接处下载完整的演示工程

DownloadSample_CS
DownloadSample_VB

 

更多ComponenOne Studio for WPF控件功能和在线资源请访问葡萄城控件官网


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网