WPF DataGrid点击有效区域Check

2021-03-12 02:42

阅读:478

标签:width   +=   return   ||   list   select   header   index   绑定   

1、DataGrid注册左键事件,点击有效区域Check

//在窗体Load中增加
this.dataGrid.AddHandler(DataGrid.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.DataGrid_MouseLeftSingleButtonDown), true);

//添加右键事件
private void DataGrid_MouseLeftSingleButtonDown(){
    if(Utility.ignoreDataGridMouseLeftSingleButtonDown(sender,e) == false){
        SelectedChange();
    }		
} 

//DataGrid 点击检查判断
//用户是否点击在了DataGrid有效区域,而不是表格内空白,滚动条,列名等无效区域
//返回值true:忽略该鼠标点击事件
public static bool ignoreDataGridMouseLeftSingleButtonDown(object sender,MouseButtonEventArgs e){
    DataGrid grid = sender as DataGrid;
    if(grid == null || (grid.DataContext == null && grid.ItemsSource == null)){
        return true;
    }
    bool isDataContextNull = false;
    if(grid.DataContext == null && grid.ItemsSource != null){
        isDataContextNull = true;
        grid.DataContext = grid.ItemsSource;
    }
    bool ignored = false;
    Point p = e.GetPosition(grid);
    if(grid.VerticalScrollBarVisibility == ScrollBarVisibility.Visible){
        if(p.X > grid.ActualWidth - SystemParameters.VerticalScrollBarWidth){
            ignored = true;
        }
    }
    
    if(ignored == false && grid.HorizontalScrollBarVisibility == ScrollBarVisibility.Visible){
        if(p.Y > grid.ActualHeight - SystemParameters.HorizontalScrollBarWidth ||
           p.Y (grid);
        if(p.Y (DependencyObject current) where T :DependencyObject{
    if(current == null) return null;
    int childCount = VisualTreeHelper.GetChildrenCount(current);
    for(int i=0; i(child);
        if(result != null) return result;        
    }
    return null;
}

WPF DataGrid点击有效区域Check

标签:width   +=   return   ||   list   select   header   index   绑定   

原文地址:https://www.cnblogs.com/DingGuo/p/14515661.html


评论


亲,登录后才可以留言!