WPF DataGrid显示按上下键移动数据、多个CheckBox勾选

1、DataGrid显示数据选中后按上下键移动数据

//xaml 在DataGrid中加入事件
PreviewKeyDown = "keyDownSetSeq"
//cs文件中加code 用System.Collections.ObjectModel的Move方法
private void keyDownSetSeq(object sender, KeyEventArgs e)
{
    int count = dataGrid.Items.Count;
    int index = dataGrid.SelectedIndex;
    if(index < 0) return;
    if(e.Key == Key.Up){
        if(index-1 < 0){
            e.Handled = true;//设为true,表示事件已经处理了,那么keypress事件将会取消
            return;
        }
        SelectLotListTemp.Move(index, index-1);//SelectLotListTemp得到的数据源
        dataGrid.DataContext = null;
        dataGrid.DataContext = SelectLotListTemp;
        dataGrid.SelectedIndex = index - 1;
    }
    if(e.Key == Key.Down){
        if(index +1 >= count){
            e.Handled = true;
            return;
        }
        SelectLotListTemp.Move(index, index+1);//SelectLotListTemp得到的数据源
        dataGrid.DataContext = null;
        dataGrid.DataContext = SelectLotListTemp;
        dataGrid.SelectedIndex = index + 1;
    }
}
    
    


2、DataGrid显示数据排序 使用System.Collection.Generic里面的Sort方法排序

3、界面多个CheckBox勾选

CheckBox checkBox = sender as CheckBox;
foreach(var c in grid.Children){
    if(c is CheckBox){
        CheckBox temp = (CheckBox)c;
        temp.IsChecked = true;
    }
}
原文地址:https://www.cnblogs.com/DingGuo/p/14297442.html