d.moncada
d.moncada

Reputation: 17402

Programmatically adding a datagrid column with a custom header to DataGrid

Within my Windows.Resources I have the following column defined:

    <DataGridTextColumn x:Key="CustomColumn" x:Shared="False">
        <DataGridTextColumn.Header>
            <StackPanel>
                <Label Padding="0" Name="labelA"/>
                <Separator HorizontalAlignment="Stretch"/>
                <Label Padding="0" Name="labelB"/>
            </StackPanel>
        </DataGridTextColumn.Header>
    </DataGridTextColumn>

I have an event that gets fired from my ViewModel and adds the following "CustomColumn" to my DataGrid:

            var column = FindResource("CustomColumn") as DataGridTextColumn;
            var label = FindName("labelA") as Label;
            label.Content = string.Format("A {0}", i);
            DataGrid1.Columns.Add(column); 

The question is, how would I change the content of the two labels inside the CustomColumn header? I above code fails because it is unable to find "labelA". (adding the column works, but I also need to set these labels). My guess is, I need to find it through the VisualTree -- but I want to make sure I'm not doing anything else wrong.

Thanks for the help.

Upvotes: 2

Views: 1149

Answers (1)

Rachel
Rachel

Reputation: 132548

I created some Visual Tree helpers that I use all the time to find objects in the Visual Tree

For example, you can find a Label named "LabelA" with this:

VisualTreeHelpers.FindChild<Label>(column, "LabelA");

Here's the FindChild method in case the above link doesn't work

using System.Windows;
using System.Windows.Media;

namespace MyNamespace
{
    public class VisualTreeHelpers
    {
        /// <summary>
        /// Looks for a child control within a parent by name
        /// </summary>
        public static T FindChild<T>(DependencyObject parent, string childName)
        where T : DependencyObject
        {
            // Confirm parent and childName are valid.
            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);
                // If the child is not of the request child type child
                T childType = child as T;
                if (childType == null)
                {
                    // recursively drill down the tree
                    foundChild = FindChild<T>(child, childName);

                    // If the child is found, break so we do not overwrite the found child.
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null &amp;&amp; frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = (T)child;
                        break;
                    }
                    else
                    {
                        // recursively drill down the tree
                        foundChild = FindChild<T>(child, childName);

                        // If the child is found, break so we do not overwrite the found child.
                        if (foundChild != null) break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }
    }
}

Upvotes: 2

Related Questions