【问题标题】:Checked items of checkbox of datagridview c#datagridview c#复选框的选中项
【发布时间】:2018-07-28 21:38:01
【问题描述】:

我在datagridview中添加了复选框,我想检查一个项目是否被选中,然后读取内容值,但我有点困惑如何完成它。
这是Xmal代码

<DataGrid.Columns>
    <DataGridTemplateColumn Header="#">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox x:Name="checkboxinstance" Checked="checked_it" Unchecked="unchecked_it" content ="{Binding apiName }" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

这是后面的代码:

private void checked_it(object sender, RoutedEventArgs e)
{
    List<CheckBox> checkBoxlist = new List<CheckBox>();
    foreach (CheckBox c in checkBoxlist)
    {
        //what I add here
    }
}

【问题讨论】:

  • 您想知道 CheckBox 当前是否被选中,还是只想选中或取消选中?
  • 我想知道 CheckBox 当前是否被选中
  • 当您的 CheckBox 被称为“checkboxinstance”时,请使用 IsChecked 属性。在您可以访问复选框的 C# 代码中: if (this.checkboxinstance.Ischecked) { // do something here }

标签: c# wpf checkbox datagridview


【解决方案1】:

您可以使用IsChecked 属性来检查复选框是否已被勾选。 要读取 Content 的值,您必须将类型转换为 TextBlock

foreach (CheckBox c in checkBoxlist)
{
     If (c.IsChecked == true)
        {
        //Code when checkbox is checked
        var _tempTBL = (TextBlock) c.Content; //Get handle to TextBlock
        var foo = _tempTBL.Text; //Read TextBlock's text
        //foo is now a string of the checkbox's content
        }
}

MSDN link to the IsChecked property

MSDN link to the Content property

【讨论】:

  • 我知道这一点,但我想得到检查项目的价值
  • 我编辑帖子,现在复选框有一些内容,如果复选框被选中,我想获取这些内容
  • 在datagridview中,如果我检查一行,那么我如何获得该行单元格的值
  • 谢谢,但有一点问题 foo 包含对象类型的值,但我需要字符串类型的值
  • 似乎您需要按照stackoverflow.com/questions/41129614/wpf-read-checkbox-content 转换为 TextBlock
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 2011-02-17
相关资源
最近更新 更多