c# - 如何遍历 WPF 工具包 Datagrid 的行

标签 c# wpf datagrid wpfdatagrid wpftoolkit

我有下一个代码,我在其中定义了一个名为 dgQuery 的 WPF 工具包数据网格控件;我用数据集的信息填充了这个,然后我在 dgQuery 中插入了一个新的复选框列来检查/取消选中一些行,我展示了我的 C# 代码的一部分:

dgQuery.DataContext = dS.Tables[0];

DataGridTemplateColumn cbCol = new DataGridTemplateColumn();
cbCol.Header = "Opc";
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(CheckBox));
Binding bind = new Binding("IsSelected");
bind.Mode = BindingMode.TwoWay;
factory.SetValue(CheckBox.IsCheckedProperty, bind);
DataTemplate cellTemplate = new DataTemplate();
cellTemplate.VisualTree = factory;
cbCol.CellTemplate = cellTemplate;
dgQuery.Columns.Insert(0, cbCol);

选中/取消选中 dgQuery 行的新复选框列后,我将单击一个按钮,仅将我选中的行保存到数据库中。问题是,我如何开发循环来读取 dgQuery 的所有行以及让我知道哪些行的复选框已选中/未选中的条件?请帮我举个例子。

谢谢!!

最佳答案

这将在您的数据网格中返回一个“行”

public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
            if (null != row) yield return row;
        }
    }

在 wpf datagrid 中,行是 ItemSource.items...没有 Rows 属性!

希望这有助于...

关于c# - 如何遍历 WPF 工具包 Datagrid 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1934529/

相关文章:

c# - 将 DataGrid 的 ItemsSource 绑定(bind)到列表

c# - NHibernate 中的多对多删除级联

c# - ASP.NET在运行时显示和隐藏图像

c# - Wpf Prism 在导航后处理 ViewModel

wpf - 边界重叠问题

c# - 如何将数据集中的表绑定(bind)到 C# 和 XAML 中的 WPF 数据网格

c# - 在已完成的事件 Windows 8 App 中从 Storyboard 获取 UIElement

c# - 模板方法模式的这个示例是否有标准命名约定?

wpf - VS2012 - 将 WPF 现有用户控件添加到项目

jsf - 如何在JSF panelGrid中添加一个空元素?