ComponentOne Studio for WPF 为我们提供了一个非常实用的类C1DragDropManager ,使用它我们可以轻松处理程序与用户交互时的拖拽操作。使用C1DragDropManager类我们只需进行简单的配置,配置完成后,对拖拽操作的支持即刻被添加到您的应用中。
通过使用该类,我们能开启应用中对拖拽命令的支持,对其进行定制,并允许移动及复制操作。本篇博客的目的,在于讨论在两个C1DataGrid 控件间拖拽多行数据的问题。
C1DataGrid 允许最终用户从一个该种控件中选定任意行内容轻松通过拖拽操作将内容传到另一个C1DataGrid控件。在选定及拖拽移动过程中,控件都会提供标识帮助用户确定选定区域范围和目标放置区域。
将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);};
接下来定制DragStart 和 DragDrop 事件来完成我们所要求的操作。
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控件功能和在线资源请访问葡萄城控件官网