这是我的选中DataGridCheckBoxColumn列的代码,请问大家有什么办法获取到选中为true的行数吗private void DgAccountManage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cntr = dgAccountManage.ItemContainerGenerator.ContainerFromIndex(dgAccountManage.SelectedIndex);
DataGridRow ObjROw = (DataGridRow)cntr;
if (ObjROw != null)
{
FrameworkElement objElement = dgAccountManage.Columns[0].GetCellContent(ObjROw);
if (objElement != null)
{
if (objElement.GetType().ToString().EndsWith("CheckBox"))
{
System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
if (objChk.IsChecked == true)
{
objChk.IsChecked = false; }
else
objChk.IsChecked = true;
} }
}
}
{
var cntr = dgAccountManage.ItemContainerGenerator.ContainerFromIndex(dgAccountManage.SelectedIndex);
DataGridRow ObjROw = (DataGridRow)cntr;
if (ObjROw != null)
{
FrameworkElement objElement = dgAccountManage.Columns[0].GetCellContent(ObjROw);
if (objElement != null)
{
if (objElement.GetType().ToString().EndsWith("CheckBox"))
{
System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
if (objChk.IsChecked == true)
{
objChk.IsChecked = false; }
else
objChk.IsChecked = true;
} }
}
}
var count = list.Count(m=>m.IsSelected);
if(count>1){
// TODO 按钮失效
}
IsSelected表示绑定到column的属性。把判断加到当前选中的行对象的set方法中即可。 而且selectionchanged是针对datagrid,并不是完全针对checkbox。
给下XAML
我想你是给错了你需要学会举一反三弄这个他得从头理清MVVM