简体   繁体   English

如何在WPF Gridview中获取检查的行值

[英]How to get checked row values in WPF Gridview

I have created gridview in WPF with two columns. 我在WPF中用两列创建了gridview。 One for DataGridTextColumn and another one is DataGridCheckBoxColumn. 一个用于DataGridTextColumn,另一个用于DataGridCheckBoxColumn。

<DataGrid AutoGenerateColumns="False" Name="dataGrid1">

 <DataGrid.Columns>
                <DataGridTextColumn Header="MyString" Binding="{Binding MyString}" />
                <DataGridCheckBoxColumn Header="MyBool" Binding="{Binding MyBool}" />
            </DataGrid.Columns>
        </DataGrid>

Now, I want to get the checked items text value, 现在,我想获取选中项目的文本值,

var SelectedList = new List<checkedBoxIte>();
            StringBuilder aa = new StringBuilder();
            for (int i = 0; i < dataGrid1.Items.Count; i++)
            {
                var item = dataGrid1.Items[i];
                var mycheckbox = dataGrid1.Columns[1].GetCellContent(item) as CheckBox;
                var myTextBox = dataGrid1.Columns[0].GetCellContent(item) as TextBox;
                if ((bool)mycheckbox.IsChecked)
                {
                    SelectedList.Add(item[i]);
                    //aa.Append(myTextBox.Text);
                }
            }

How, can i do this. 我怎样才能做到这一点。

You should not be using a TextBox , you should use a TextBlock instead, because that's what your DataGridView actually contains in WPF . 您不应该使用TextBox ,而应该使用TextBlock ,因为那是您的DataGridView实际上包含在WPF

So change your code like this: 因此,像这样更改代码:

var SelectedList = new List<checkedBoxIte>();
StringBuilder aa = new StringBuilder();
for (int i = 0; i < dataGrid1.Items.Count; i++)
{
    var item = dataGrid1.Items[i];
    var mycheckbox = dataGrid1.Columns[1].GetCellContent(item) as CheckBox;
    var myTextBlock = dataGrid1.Columns[0].GetCellContent(item) as TextBlock;
    if ((bool)mycheckbox.IsChecked)
    {
        SelectedList.Add(item[i]);
        aa.Append(myTextBlock.Text);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM