DataGridCheckBoxColumn-如何返回检查的行? [英] DataGridCheckBoxColumn - How to return checked rows?

查看:47
本文介绍了DataGridCheckBoxColumn-如何返回检查的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,我有一个datagrid,其中填充了数据表中的数据(作为sql数据的返回),此外,我还插入了CheckBoxColumn.显示数据有效.现在,我想将应用程序"列的所有值打印到控制台,以检查CheckBoxolumn的复选框.如何运作?

In WPF I have a datagrid which I populate with data from a datatable (as a return of sql data) plus, I inserted a CheckBoxColumn. Displaying the data works. Now I want to print all values of column "Application" to console where a checkbox of CheckBoxolumn is checked. How does this work?

WPF代码段:

     <DataGrid Name="AvailableApps" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" Margin="5,30,5,5" ColumnWidth="*" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Application" Binding="{Binding Application}"/>
            <DataGridCheckBoxColumn Header="Selection"/>
        </DataGrid.Columns>
    </DataGrid>

C#代码段:

        AvailableApps.ItemsSource = AppsAvailable.AsDataView();
        AvailableApps.IsReadOnly = false;
        AvailableApps.Columns[1].IsReadOnly = true;

推荐答案

还没有运行代码,但是我会做一些类似的事情(使用LINQ):我已经部分复制了OP的代码,因为我没有 AppsAvailable 的结构,所以我创建了一个简单的 Tuple< string,bool>的 ObservableCollection ; 并将其用作商品来源.

Didn't run the code yet, but I'd do something like (use LINQ): I've partly replicated the OP's code, since I don't have the structure of AppsAvailable, I've created a simple ObservableCollection of Tuple<string, bool> and used it as the items source.

    AvailableApps.Items.OfType<Tuple<string, bool>>()
        .Where(i => i.Item2 == true)
        .Select(i => i.Item1)
        .ToList()
        .ForEach(i => Console.WriteLine($"{i}"));

它的作用:

  • 它接受 DataGrid 的所有项目并将其强制转换为合适的类型(在OP的情况下,它应为 OfType< typeOf(AppsAvailable)> 或更佳实际类型)
  • 仅对选中了复选框(显式条件,更易读)的元组进行过滤-如果是OP,则应检查其填充复选框列的条件
  • 提取字符串值-如果是OP,则应使用具有所请求内容的组件
  • 在控制台中一一打印(已准备好字符串格式).
  • it takes all the items of the DataGrid and casts them to the suitable type (in the OP's case it should be OfType<typeOf(AppsAvailable)> or better the actual type)
  • filters the tuples for only those where checkbox was checked (explicit condition, more readability) - in case of the OP they should check for the condition they populate the checkbox column
  • extracts the string values - in case of the OP they should use the component with the requested content
  • prints one by one in the console (already string-formatting ready).

第二行至关重要,因为第二行将提取子级列的值,因此,如果代码不能立即使用,这是您需要对其进行调整的地方.

这篇关于DataGridCheckBoxColumn-如何返回检查的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆