我想从DataGrid
获取/计算所有已检查项目。我在DataGrid
中有20个项目,如下图所示。在20个可见项目中,有14个或13个可见项目。当我通过单击全选按钮将所有项目标记为选中时,所有20个项目都被标记为选中状态。但是当我通过单击打印按钮来计数已检查项目时,计数显示它仅标记14.怎么回事?友善的帮助
Datagrid:
<DataGrid HorizontalAlignment="Left" Height="265" Margin="10,74,0,0"
Name="GridTable" AutoGenerateColumns="False" VerticalAlignment="Top" Width="766" CanUserAddRows="False" IsReadOnly="False" SelectionMode="Single" SelectionUnit="Cell">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="#0073c4"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Select" Binding="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Code" Width="1*" IsReadOnly="True" Binding="{Binding Code}"/>
<DataGridTextColumn Header="Name" Width="1*" IsReadOnly="True" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
全选按钮:
private void SelectAllBtn_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < GridTable.Items.Count; i++)
{
var item = GridTable.Items[i];
Item itemm = (Item)GridTable.Items[i];
itemm.IsSelected = true;
var mycheckbox = GridTable.Columns[0].GetCellContent(item) as CheckBox;
if (mycheckbox != null)
{
mycheckbox.IsChecked = true;
}
}
}
打印按钮单击:
private void Print_Click(object sender, RoutedEventArgs e)
{
int count = 0;
for (int i = 0; i < GridTable.Items.Count; i++)
{
var item = GridTable.Items[i];
if (GridTable.Columns[0].GetCellContent(item) as CheckBox != null)
{
var mycheckbox = GridTable.Columns[0].GetCellContent(item) as CheckBox;
if ((bool)mycheckbox.IsChecked)
{
count++;
listTobePrint.Add(tableList[i]);
}
}
}
MessageBox.Show(count.ToString());
注意:此MessageBox.Show(count.ToString());
数14
Item.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inventory_Control.Classes
{
class Item : INotifyPropertyChanged
{
private string id;
private string code;
private string name;
private string description;
private string quantity;
private string availableQuantity;
private string unitprice;
private string subtotal;
private string category;
private string type;
private string location;
private string index;
private bool _isSelected;
public string Id
{
get { return id; }
set
{
id = value;
NotifyPropertyChanged("Id");
}
}
public string Code
{
get { return code; }
set
{
code = value;
NotifyPropertyChanged("Code");
}
}
public string Name
{
get { return name; }
set
{
name = value;
NotifyPropertyChanged("Name");
}
}
public string Description
{
get { return description; }
set
{
description = value;
NotifyPropertyChanged("Description");
}
}
public string Quantity
{
get { return quantity; }
set
{
quantity = value;
NotifyPropertyChanged("Quantity");
}
}
public string AvailableQuantity
{
get { return availableQuantity; }
set
{
availableQuantity = value;
NotifyPropertyChanged("AvailableQuantity");
}
}
public string UnitPrice
{
get { return unitprice; }
set
{
unitprice = value;
NotifyPropertyChanged("UnitPrice");
}
}
public string SubTotal
{
get { return subtotal; }
set
{
subtotal = value;
NotifyPropertyChanged("SubTotal");
}
}
public string Category
{
get { return category; }
set
{
category = value;
NotifyPropertyChanged("Category");
}
}
public string Type
{
get { return type; }
set
{
type = value;
NotifyPropertyChanged("Type");
}
}
public string Location
{
get { return location; }
set
{
location = value;
NotifyPropertyChanged("Location");
}
}
public string Index
{
get { return index; }
set
{
index = value;
NotifyPropertyChanged("Index");
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
答案 0 :(得分:2)
这是因为您的DataGrid使用行虚拟化。它告诉您真相-它只有14个选定的行,因为这就是呈现基础数据所需的全部。
CODE杂志的文章XAML Anti-Patterns: Virtualization有一个很好的解释。
我注意到您尚未对DataGrid进行数据绑定,您必须以编程方式添加行吗?访问所选项目的正确(正确)方法是,进入包含绑定了DataGrid的Item
对象集合的视图模型,并对该集合进行迭代以找到所选项目。