禁用WPF中DataGrid默认的鼠标左键拖动多选行的效果
时间:2015-11-12 13:43:19
收藏:0
阅读:1021
最近项目上有需求要做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 );
}
}
版权声明:本文为博主原创文章,欢迎转载,仅请署名
原文:http://blog.csdn.net/muzizongheng/article/details/49796655
评论(0)