最近项目上有需求要做DataGrid的行的拖拽功能, 有个很现实的问题就是鼠标左键按下是拖拽还是多选。
查看了DataGrid的源码发现,系统内部会在鼠标按下的时候CaptureMouse,然后设置私有变量来保存多选标志, 在鼠标MouseMove的时候根据变量判断是否多选。
private bool _isDraggingSelection; // Whether a drag select is being performed
分析清楚了, 解决就好办了。我们只有在DataGrid的PreviewMouseMove里利用反射拿到这个私有变量,设置为false即可。
private void DisableRowDraggingSelection( DataGrid dataGrid )
{
//Set _isDraggingSelection and disable system native drag selection feature.
var property = typeof ( DataGrid ).GetField (
"_isDraggingSelection" ,
BindingFlags .Instance | BindingFlags. NonPublic | BindingFlags . IgnoreCase);
if (property != null)
{
property .SetValue ( dataGrid, false );
}
}