Question

I have a nested ItemsControl to display the following Model:

public class Parent
{
    public string ParentTitle 
    { 
       get;
       set;
    }

    ICollection<Child> Children
    { 
       get;
       set;
    } 
}

public class Child
{
    public string ChildTitle 
    {
       get; 
       set;
    }
}

The ItemsControl looks like this:

<ItemsControl x:Name="listOfParents">
     <ItemsControl.ItemTemplate>
          <DataTemplate DataType="{x:Type local:Parent}">
               <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Button x:Name="btnTarget" Grid.Row="0" Content="{Binding ParentTitle}"></Button>

                    <ItemsControl Grid.Row="1" ItemsSource="{Binding Children}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="{x:Type local:Child}">
                                <Button x:Name="btnSource" Content="{Binding ChildTitle}" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
               </Grid>
          </DataTemplate>
     </ItemsControl.ItemTemplate>
</ItemsControl>

The listOfParents Itemssouce is List<Parent>. How do I access Button btnTarget when btnSource is clicked?

Was it helpful?

Solution

You can access the Button with the FindChild():

Listing of function:

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

        T foundChild = null;

        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))
                {
                    var frameworkElement = child as FrameworkElement;

                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        foundChild = (T)child;
                        break;
                    }
                    else
                    {
                        foundChild = FindChild<T>(child, childName);

                        if (foundChild != null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    foundChild = (T)child;
                    break;
                }
        }

        return foundChild;
    }

The call is made so:

    private void btnSource_Click(object sender, RoutedEventArgs e)
    {
        Button MyBtnTarget = FindChild<Button>(listOfParents, "btnTarget");

        MessageBox.Show(MyBtnTarget.Content.ToString());        
    }

But in this way, the function will select the very first button, and we need to get access to all the elements. For this, I rewrote the function so that it returns all the elements of the list. Here's the code:

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

Calling function:

    private void btnSource_Click(object sender, RoutedEventArgs e)
    {            
        // Create the List of Button
        List<Button> list = new List<Button>();

        // Find all elements
        FindChildGroup<Button>(listOfParents, "btnTarget", ref list);
        string text = "";

        foreach (Button elem in list)
        {
            text += elem.Content.ToString() + "\n";
        }

        MessageBox.Show(text, "Text in Button");
    }   

In general there are several ways to access the template. Here's one: How to use FindName with a ContentControl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top