1 /// <summary> 2 /// 设置datagrid选中多行 3 /// </summary> 4 /// <param name="listIndex"></param> 5 private void SetSelectMessageIndex(List<int> listIndex) 6 { 7 try 8 { 9 foreach(var i in listIndex) 10 { 11 if (i >= datagrid.Items.Count) 12 continue; 13 14 DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(i); 15 row.IsSelected = true; 16 } 17 } 18 catch(Exception ex) 19 { 20 Common.LogFile.LogErr(ex.ToString()); 21 } 22 }
在界面后台代码中设置要选中的行的序号,若是MVVM绑定则需通过事件触发
1 private void Win_Loaded(object sender, RoutedEventArgs e) 2 { 3 var vm = this.DataContext as ViewModel; 4 if (vm != null) 5 { 6 vm.SetSelectMessageIndexEvent += SetSelectMessageIndex; 7 } 8 }
标签:多行,datagrid,vm,private,DataGrid,选中,后台,WPF From: https://www.cnblogs.com/ppcc/p/18136557