WPF DataGrid顯示按上下鍵移動資料、多個CheckBox勾選
阿新 • • 發佈:2021-01-19
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;
}
}