0

i want to get the value of CheckBoxColumn in DataGrid wpf

i try this code

foreach (spShowTotal_Result item in dgShowStudent.ItemsSource)
        {

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

        }

but this exception appear

Unable to cast object of type 'System.Windows.Controls.ContentPresenter' to type 'System.Windows.Controls.CheckBox'.

Ahmed Rabie
  • 21
  • 1
  • 8
  • no in mvvm , just wpf – Ahmed Rabie Jul 04 '16 at 06:36
  • Did you tried [this](http://stackoverflow.com/a/6100061/2819451) – Gopichandar Jul 04 '16 at 06:40
  • var cell = dataGrid.GetCell(5, 0); GetCellmethod doesn't there !!! – Ahmed Rabie Jul 04 '16 at 06:51
  • `var cp = ((ContentPresenter)dgShowStudent.Columns[0].GetCellContent(item)).Content;` `var checkbox = (CheckBox)cp.ContentTemplate.FindName("root", cp);` `bool? check = checkbox.IsChecked;` – Gopichandar Jul 04 '16 at 06:56
  • syntax error in "ContentTemplate" 'object' does not contain a definition for 'ContentTemplate' and no extension method 'ContentTemplate' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) – Ahmed Rabie Jul 04 '16 at 07:06

1 Answers1

2

Seems like the workaround provided in comments not working out for you. Let me solve this in a different way.

Consider DataGrid as

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

and in your xaml.cs, you can access like below

  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
      }      
  }

You need the following class to iterate through the tree.

    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;
    }

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

Community
  • 1
  • 1
Gopichandar
  • 2,742
  • 2
  • 24
  • 54