logo Devexpress WPF控件文档中心

行/单元格拖拽


GridControl支持本地拖放,允许你的终端用户。
  • 在所有的视图类型中拖放记录(表视图,树形列表视图,卡片视图)。
  • 在主细节视图中拖放记录。
  • 在网格控件和外部控件(DevExpress WPF控件和标准WPF控件)之间拖放记录。
  • 在外部应用程序中拖放记录。

拖拽工功能是需要启用属性的,GridControl v17.2及以后的版本都支持本地拖放功能。在以前的版本中,使用拖放管理器来启用拖放功能。


<dxg:GridControl>
   <!---->        
   <dxg:GridControl.View>
      <dxg:TableView AllowDragDrop="True" />
   </dxg:GridControl.View>
</dxg:GridControl>
拖拽相关选项和事件请参照:The GridControl has a set of properties and events that allow you to control drag-and-drop operations at different stages.


拖拽的4大注意事项:

1. 防止被拖拽

(1)设定拖拽事件

<dxg:TreeListView AllowDragDrop="True" StartRecordDrag="OnStartRecordDrag" />
(2).将 StartRecordDragEventArgs.AllowDrag 属性设置为 false,以防止记录被拖动。

(3). 后端代码

void OnStartRecordDrag(object sender, StartRecordDragEventArgs e) {
    if (e.Records.Any(x => view.GetNodeByContent(x).HasChildren)) {
        e.AllowDrag = false;
        e.Handled = true;
    }
}
2. 防止被拖入其他记录

(1)设置事件

<dxg:TreeListView AllowDragDrop="True" DragRecordOver="OnDragRecordOver" />
(2)将Effects属性设置为DragDropEffects.None,使投放目标拒绝数据。

(3)后端代码:

void OnDragRecordOver(object sender, DragRecordOverEventArgs e) {
    string position = ((Employee)e.TargetRecord).Position;
    if (position == "President" || position == "Vice President") {
        e.Effects = DragDropEffects.None;
        e.Handled = true;
    }
}
3. 自定义拖拽项目

(1)设置事件

<dxg:TreeListView AllowDragDrop="True" DropRecord="OnDropRecord" />
(2)获取和自定义数据

void OnDropRecord(object sender, DropRecordEventArgs e) {
    object data = e.Data.GetData(typeof(RecordDragDropData));
    foreach (Employee employee in ((RecordDragDropData)data).Records) {
        employee.Position = ((Employee)e.TargetRecord).Position;
        employee.Department = ((Employee)e.TargetRecord).Department;
    }
    if (e.DropPosition == DropPosition.Inside) {
        foreach(Employee employee in ((RecordDragDropData)data).Records) {
            employee.Position = "";
        }
    }
}
4. 防止拖动的项目被删除

(1) 设置事件

<dxg:TableView AllowDragDrop="True" CompleteRecordDragDrop="OnCompleteRecordDragDrop" />
(2)后端代码

void OnCompleteRecordDragDrop(object sender, CompleteRecordDragDropEventArgs e) {
    e.Handled = true;
}



在线咨询
联系我们

客服热线
023-68661681

QQ客服

意见反馈


添加微信获专业服务

TOP