以编程方式将带有自定义标头的datagrid列添加到DataGrid

时间:2011-11-10 18:09:27

标签: c# .net wpf mvvm datagrid

在我的Windows.Resources中,我定义了以下列:

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

我有一个从我的ViewModel触发的事件,并将以下“CustomColumn”添加到我的DataGrid:

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

问题是,如何更改CustomColumn标题内的两个标签的内容?我上面的代码失败,因为它无法找到“labelA”。 (添加列有效,但我还需要设置这些标签)。我的猜测是,我需要通过VisualTree找到它 - 但我想确保我没有做任何其他错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我创建了一些Visual Tree helpers,我一直用它来查找Visual Tree中的对象

例如,您可以使用以下命令找到名为“LabelA”的标签:

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

如果上述链接不起作用,则为FindChild方法

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