原文: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 = ; i < this.dgList.Items.Count; i++)
{
var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i);
DataGridRow ObjROw = (DataGridRow)cntr;
if (ObjROw != null)
{
FrameworkElement objElement = dgList.Columns[].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 name="sender"></param>
/// <param name="e"></param>
private void btnQuanFou_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i < this.dgList.Items.Count; i++)
{
var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i);
DataGridRow ObjROw = (DataGridRow)cntr;
if (ObjROw != null)
{
FrameworkElement objElement = dgList.Columns[].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 name="sender"></param>
/// <param name="e"></param>
private void btnFanXuan_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i < this.dgList.Items.Count; i++)
{
var cntr = dgList.ItemContainerGenerator.ContainerFromIndex(i);
DataGridRow ObjROw = (DataGridRow)cntr;
if (ObjROw != null)
{
FrameworkElement objElement = dgList.Columns[].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();
}
05-08 08:02