【问题标题】:loop through checkboxcolumn in datagrid in wpf checked or not循环遍历 wpf 中数据网格中的复选框列是否选中
【发布时间】:2016-07-04 05:29:08
【问题描述】:

我想在 DataGrid wpf 中获取 CheckBoxColumn 的值

我试试这个代码

foreach (spShowTotal_Result item in dgShowStudent.ItemsSource)
        {

            bool? check = ((CheckBox)dgShowStudent.Columns[0].GetCellContent(item)).IsChecked;

        }

但是出现了这个异常

无法将“System.Windows.Controls.ContentPresenter”类型的对象转换为“System.Windows.Controls.CheckBox”类型。

【问题讨论】:

  • 在 mvvm 中没有,只是 wpf
  • 你试过this
  • var cell = dataGrid.GetCell(5, 0); GetCellmethod 不存在!!!
  • var cp = ((ContentPresenter)dgShowStudent.Columns[0].GetCellContent(item)).Content; var checkbox = (CheckBox)cp.ContentTemplate.FindName("root", cp); bool? check = checkbox.IsChecked;
  • “ContentTemplate”中的语法错误“对象”不包含“内容模板”的定义,并且找不到接受“对象”类型的第一个参数的扩展方法“内容模板”(您是否缺少使用指令还是程序集引用?)

标签: wpf datagrid


【解决方案1】:

似乎 cmets 中提供的解决方法不适合您。让我以不同的方式解决这个问题。

DataGrid 视为

<DataGrid x:Name="datagridexec">
    <DataGridTemplateColumn Header="DUT">
         <DataGridTemplateColumn.CellTemplate>                                                                   
               <DataTemplate>
                    <CheckBox x:Name="checkboxinstance"/>
               </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>
</DataGrid>

在你的xaml.cs,你可以像下面这样访问

  List<CheckBox> checkBoxlist = new List<CheckBox>();

  // Find all elements
  FindChildGroup<CheckBox>(datagridexec, "checkboxinstance", ref checkBoxlist );

  foreach (CheckBox c in checkBoxlist)
  {
      if (c.IsChecked)
      {
           //do whatever you want
      }      
  }

您需要以下类来遍历树。

    public static void FindChildGroup<T>(DependencyObject parent, string childName, ref List<T> list) where T : DependencyObject
    {
        // Checks should be made, but preferably one time before calling.
        // And here it is assumed that the programmer has taken into
        // account all of these conditions and checks are not needed.
        //if ((parent == null) || (childName == null) || (<Type T is not inheritable from FrameworkElement>))
        //{
        //    return;
        //}

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

        for (int i = 0; i < childrenCount; i++)
        {
            // Get the child
            var child = VisualTreeHelper.GetChild(parent, i);

            // Compare on conformity the type
            T child_Test = child as T;

            // Not compare - go next
            if (child_Test == null)
            {
                // Go the deep
                FindChildGroup<T>(child, childName, ref list);
            }
            else
            {
                // If match, then check the name of the item
                FrameworkElement child_Element = child_Test as FrameworkElement;

                if (child_Element.Name == childName)
                {
                    // Found
                    list.Add(child_Test);
                }

                // We are looking for further, perhaps there are
                // children with the same name
                FindChildGroup<T>(child, childName, ref list);
            }
        }

        return;
    }

参考:How to access datagrid template column textbox text WPF C#

【讨论】:

    猜你喜欢
    • 2018-08-08
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2023-03-20
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多