WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选)

来源:转载

前台代码

 <DataGrid.Columns> <DataGridCheckBoxColumn Header="选择" Binding="{Binding Path=RUID}" x:Name="cRUID"></DataGridCheckBoxColumn> <DataGridTextColumn Header="结算类型" Binding="{Binding Path=CTYP}" x:Name="cCTYP" IsReadOnly="True" ></DataGridTextColumn> </DataGrid.Columns>

后台:c#代码

 /// /// 全选 /// /// /// private void btnQuanXuan_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < this.dgList.Items.Count; i++) { var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i); DataGridRow ObjROw = (DataGridRow)cntr; if (ObjROw != null) { FrameworkElement objElement = dgList.Columns[0].GetCellContent(ObjROw); if (objElement != null) { //if (objElement.GetType().ToString().EndsWith("cRUID")) //{ System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; if (objChk.IsChecked == false) { objChk.IsChecked = true; } //} } } } } /// <summary>

/// 全否

 /// </summary> /// <param></param> /// <param></param> private void btnQuanFou_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < this.dgList.Items.Count; i++) { var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i); DataGridRow ObjROw = (DataGridRow)cntr; if (ObjROw != null) { FrameworkElement objElement = dgList.Columns[0].GetCellContent(ObjROw); if (objElement != null) { //if (objElement.GetType().ToString().EndsWith("cRUID")) //{ System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; if (objChk.IsChecked == true) { objChk.IsChecked = false; } //} } } } } /// <summary> /// 反选 /// </summary> /// <param></param> /// <param></param> private void btnFanXuan_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < this.dgList.Items.Count; i++) { var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i); DataGridRow ObjROw = (DataGridRow)cntr; if (ObjROw != null) { FrameworkElement objElement = dgList.Columns[0].GetCellContent(ObjROw); if (objElement != null) { //if (objElement.GetType().ToString().EndsWith("cRUID")) //{ System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; if (objChk.IsChecked == false) { objChk.IsChecked = true; } else { objChk.IsChecked = false; } //} } } } }

使用模板列全选:

前台代码:

 <DataGridCheckBoxColumn Binding="{Binding Path=RUID,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}" x:Name="cRUID"> <DataGridCheckBoxColumn.HeaderTemplate> <DataTemplate> <CheckBox x:Name="ckbSelectedAll" Content="全选" Checked="ckbSelectedAll_Checked" Unchecked="ckbSelectedAll_Unchecked" IsChecked="False" /> </DataTemplate> </DataGridCheckBoxColumn.HeaderTemplate> </DataGridCheckBoxColumn> 

后台 c#代码

:

 /// /// 全选 /// /// /// private void ckbSelectedAll_Checked(object sender, RoutedEventArgs e) { this.dgList.SelectAll(); } /// /// 全不选 /// /// /// private void ckbSelectedAll_Unchecked(object sender, RoutedEventArgs e) { this.dgList.UnselectAll(); }



分享给朋友:
您可能感兴趣的文章:
随机阅读: