0

The Generic static T FindChild(DependencyObject parent, string childName) does not find DataGrids. T childType = child as T; does always return null for DataGrids.

XAML:

<TabControl>
    <TabItem Header="TabItemHeader" FontSize="25">
        <DockPanel>
            <DataGrid Name="MyDataGrid">
            </DataGrid>
        </DockPanel>
    </TabItem>
</TabControl>

FindChild Call

DataGrid MyDataGrid;
MyDataGrid= FindChild<DataGrid>(ParentElement, "MyDataGrid");

FindChild

public static T FindChild<T>(DependencyObject parent, string childName)
        where T : DependencyObject {
            if (parent == null) return null;

            T foundChild = null;

            if (parent.GetType().ToString().Equals("System.Windows.Controls.TabControl")) {
                TabControl tabControl = parent as TabControl;
                for (int i = 0; i < tabControl.Items.Count; i++) {
                    TabItem tabItem = tabControl.Items[i] as TabItem;
                    DependencyObject dependencyObject = tabItem.Content as DependencyObject;
                    foundChild = FindChild<T>(dependencyObject, childName);

                    if(foundChild != null) {
                        break;
                    }
                }
            }
            else {
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++) {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
                    if (childType == null) {
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName)) {
                        if (VisualTreeHelper.GetChildrenCount(child) > 0) {
                            foundChild = FindChild<T>(child, childName);
                            if (foundChild != null) break;
                        }
                        var frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName) {
                            foundChild = (T)child;
                            break;
                        }
                    }
                    else {
                        foundChild = (T)child;
                        break;
                    }
                }
            }

            return foundChild;
        }

If the DataGrid is replaced by a Grid it works perfectly fine. And searched accordingly to it.

Ps:If anyone has a better solution for the TabControl I would appreciate it.

Kerubis
  • 15
  • 4
  • Did you try to use [this](https://stackoverflow.com/a/25229554/7252182) method? `var datagrid = FindVisualChildren(tabItem)?.FirstOrDefault(x => x.Name == "MyDataGrid");` – mm8 May 31 '19 at 10:03
  • Does not work either. But if I add and Grid above the DataGrid it works fine. Can someone explain why? in addition I found an Bug in my FindChild method, there was a break missing in the TabControl part (added it) – Kerubis May 31 '19 at 10:35
  • What is "ParentElement" in your sample code? Please provide an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). – mm8 May 31 '19 at 11:32
  • The Dockpanel as seen in the XAML – Kerubis May 31 '19 at 12:45

0 Answers0