以编程方式向datagrid添加带有自定义标题的数据网格列

本文关键字:标题 数据 数据网 网格 自定义 方式 编程 datagrid 添加 | 更新日期: 2023-09-27 18:14:23

在我的Windows。我定义了以下列:

    <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找到它——但我想确保我没有做错任何其他事情。

谢谢你的帮助

以编程方式向datagrid添加带有自定义标题的数据网格列

我创建了一些可视树帮助器,我一直使用它们来查找可视树中的对象

例如,你可以这样找到一个名为"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;
        }
    }
}