1 前台代码 2 3 <DataGrid.Columns> 4 <DataGridCheckBoxColumn Header="选择" Binding="{Binding Path=RUID}" x:Name="cRUID"></DataGridCheckBoxColumn> 5 <DataGridTextColumn Header="结算类型" Binding="{Binding Path=CTYP}" x:Name="cCTYP" IsReadOnly="True" > </DataGridTextColumn> 6 7 </DataGrid.Columns> 8 9 后台:c#代码 10 11 : 12 13 /// 14 /// 全选 15 /// 16 /// 17 /// 18 private void btnQuanXuan_Click(object sender, RoutedEventArgs e) 19 { 20 for (int i = 0; i < this.dgList.Items.Count; i++) 21 { 22 var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i); 23 DataGridRow ObjROw = (DataGridRow)cntr; 24 if (ObjROw != null) 25 { 26 FrameworkElement objElement = dgList.Columns[0].GetCellContent(ObjROw); 27 if (objElement != null) 28 { 29 //if (objElement.GetType().ToString().EndsWith("cRUID")) 30 //{ 31 System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; 32 if (objChk.IsChecked == false) 33 { 34 objChk.IsChecked = true; 35 } 36 //} 37 } 38 } 39 } 40 } 41 42 43 44 /// <summary> 45 /// 全否 46 /// </summary> 47 /// <param name="sender"></param> 48 /// <param name="e"></param> 49 private void btnQuanFou_Click(object sender, RoutedEventArgs e) 50 { 51 for (int i = 0; i < this.dgList.Items.Count; i++) 52 { 53 var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i); 54 DataGridRow ObjROw = (DataGridRow)cntr; 55 if (ObjROw != null) 56 { 57 FrameworkElement objElement = dgList.Columns[0].GetCellContent(ObjROw); 58 if (objElement != null) 59 { 60 //if (objElement.GetType().ToString().EndsWith("cRUID")) 61 //{ 62 System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; 63 if (objChk.IsChecked == true) 64 { 65 objChk.IsChecked = false; 66 } 67 //} 68 } 69 } 70 } 71 } 72 /// <summary> 73 /// 反选 74 /// </summary> 75 /// <param name="sender"></param> 76 /// <param name="e"></param> 77 private void btnFanXuan_Click(object sender, RoutedEventArgs e) 78 { 79 for (int i = 0; i < this.dgList.Items.Count; i++) 80 { 81 var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i); 82 DataGridRow ObjROw = (DataGridRow)cntr; 83 if (ObjROw != null) 84 { 85 FrameworkElement objElement = dgList.Columns[0].GetCellContent(ObjROw); 86 if (objElement != null) 87 { 88 //if (objElement.GetType().ToString().EndsWith("cRUID")) 89 //{ 90 System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; 91 if (objChk.IsChecked == false) 92 { 93 objChk.IsChecked = true; 94 } 95 else 96 { 97 objChk.IsChecked = false; 98 } 99 //} 100 } 101 } 102 } 103 } 104 105 106 107 使用模板列全选: 108 109 前台代码: 110 111 <DataGridCheckBoxColumn Binding="{Binding Path=RUID,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}" x:Name="cRUID"> 112 <DataGridCheckBoxColumn.HeaderTemplate> 113 <DataTemplate> 114 <CheckBox x:Name="ckbSelectedAll" Content="全选" Checked="ckbSelectedAll_Checked" Unchecked="ckbSelectedAll_Unchecked" IsChecked="False" /> 115 </DataTemplate> 116 </DataGridCheckBoxColumn.HeaderTemplate> 117 </DataGridCheckBoxColumn> 118 119 120 后台 c#代码 121 122 : 123 124 /// 125 /// 全选 126 /// 127 /// 128 /// 129 private void ckbSelectedAll_Checked(object sender, RoutedEventArgs e) 130 { 131 this.dgList.SelectAll(); 132 } 133 134 /// 135 /// 全不选 136 /// 137 /// 138 /// 139 private void ckbSelectedAll_Unchecked(object sender, RoutedEventArgs e) 140 { 141 this.dgList.UnselectAll(); 142 }