1

Ok this has been asked many times but I just can't find the right solution. I have datagrid defined like this:

<DataGrid AutoGenerateColumns="False"
      IsReadOnly="True"
      Name="InputDocItemsDataGrid"
      ItemsSource="{Binding Path= InputItems}" 
      SelectedItem="{Binding Path= InputItem, UpdateSourceTrigger=PropertyChanged}"
      SelectionChanged="InputDocItemsDataGrid_SelectionChanged"
      PreviewMouseLeftButtonDown="InputDocItemsDataGrid_PreviewMouseLeftButtonDown">
    <DataGrid.Columns>
        <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
            <DataGridTemplateColumn.HeaderTemplate>
                <DataTemplate>
                    <CheckBox Name="cbxAll" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="cbxAll_Checked" />
                </DataTemplate>
            </DataGridTemplateColumn.HeaderTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Name="cbxSingleRow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" PreviewMouseLeftButtonDown="cbxSingleRow_PreviewMouseLeftButtonDown" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Well I added only this template column here becouse it's the point of interest. So what I'm trying to manage is to access checkobx "cbxSingleRow" as it is outside of datagrid, so i would be able to do all the regular stuff with it like for example: cbxSingleRow.IsEnabled = false;

So how do I get that checkbox?

5
  • "all the regular stuff" is done via DATABINDING in WPF. not procedural code. Commented Jan 29, 2014 at 16:53
  • I'm aware of that, I just need to access that checkbox from codebehind like it is any other checkbox outside of datagrid? Is there any clear solution to get that effect? Commented Jan 29, 2014 at 16:58
  • I just need to access that checkbox from codebehind - what for? whatever you want to do, do it via DataBinding. Commented Jan 29, 2014 at 18:37
  • hi Stojdza,,,,i m facing same problem..u have solution about it? Commented Feb 14, 2015 at 5:20
  • @SANDEEP hay there! Yes I have solved it by using visual tree helper class as Rohit described in his answer but just a little bit different. Also there is another approach to access controls inside a DataGrid via databinding and using of RelativeSource property. If you need some more info let me know. Commented Mar 3, 2015 at 12:18

1 Answer 1

1

You can get that with the help of VisualTreeHelper class.

Move this method in some utility class so that can be reused.

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj,
                                         string name) where T : DependencyObject
{
    if (depObj != null)
    {
       for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
       {
          DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
          if (child != null && child is T &&
                (child as FrameworkElement).Name.Equals(name))
          {
             yield return (T)child;
          }

          foreach (T childOfChild in FindVisualChildren<T>(child, name))
          {
             yield return childOfChild;
          }
       }
    }
}

Usage:

foreach (CheckBox checkBox in UtilityFunctions.
             FindVisualChildren<CheckBox>(InputDocItemsDataGrid, "cbxSingleRow"))
{           
   checkBox.IsChecked = true;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.