如何在 WPF C# 中访问数据网格模板列文本框的文本

8

我需要从后台代码中访问DataGrid模板列中的文本,但是我不知道该怎么做。我需要在SelectionChanged事件上将文本更改为我传递的任何字符串。请问有人可以告诉我如何实现吗?我在这里找到了一个类似的问题(链接),但没有答案。

1个回答

16

在查找 DataGrid 模板列中的控件时,您应该使用 FindChild() 方法:

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

例如,我在 MyDataGrid 中有这个模板列:

在 MyDataGrid 中,我有这个模板列:

<DataGridTemplateColumn Width="1.5*" IsReadOnly="False">
    <DataGridTemplateColumn.Header>
        <TextBlock Text="Sample" ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}" FontSize="14" />
     </DataGridTemplateColumn.Header>

     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <TextBlock x:Name="MyTextBlock" Text="Hello!" />
         </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

通过代码访问,您可以:

TextBlock MyTextBlock = FindChild<TextBlock>(MyDataGrid, "MyTextBlock");

MessageBox.Show(MyTextBlock.Text);

注意: 只有在控件完全加载后才使用FindChild,否则它将找不到并返回null。 在这种情况下,我将此代码放在事件ContentRendered (Window)中,该事件表示窗口的所有内容已成功加载(即使事件MyDataGrid_Loaded无法访问MyTextBlock,因为它尚未加载)

    private void Window_ContentRendered(object sender, EventArgs e)
    {
        TextBlock MyTextBlock = FindChild<TextBlock>(MyDataGrid, "MyTextBlock");

        MessageBox.Show(MyTextBlock.Text);
    }

编辑1:

为了让选择的行的控件添加事件SelectionChangedDataGrid中,需要编写一个函数,该函数将返回选定的行:

    private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            var row_list = GetDataGridRows(MyDataGrid);

            foreach (DataGridRow single_row in row_list)
            {
                if (single_row.IsSelected == true)
                {
                    TextBlock MyTextBlock = FindChild<TextBlock>(single_row, "MyTextBlock");

                    MessageBox.Show(MyTextBlock.Text);
                }
            }
        }

        catch 
        {
            throw new Exception("Can't get access to DataGridRow");
        }
    }

GetDataGridRows()函数的列表:

    public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;

        if (null == itemsSource)
        {
            yield return null; 
        }

        foreach (var item in itemsSource)
        {
            var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;

            if (null != row)
            {
                yield return row; 
            }
        }
    }

编辑2:

为了获取所有项目,我重写了函数FindChild():

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

调用这个新函数:
   private void Window_ContentRendered(object sender, EventArgs e)
   {
        // Create the List
        List<TextBlock> list = new List<TextBlock>();

        // Find all elements
        FindChildGroup<TextBlock>(MyDataGrid, "MyTextBlock", ref list);
        string text = "";

        // Print
        foreach (TextBlock elem in list)
        {
            text += elem.Text + "\n";
        }

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

一般来说,这种做法并不是最好的...如果你想要获取项目(如全部或选择的项目),你可以直接联系存储数据的列表(比如 ObservableCollection)。此外,像 PropertyChanged 这样的事件也很有用。


我遇到了与我链接的帖子中的人一样的问题。我找不到让它在每一行上都起作用的方法,只有一个。如何使其在选定的行上工作? - acosta
嗯...获取它们所有的代码可以运行,但是第一个编辑的代码会抛出无法访问DataGridRow的异常。 - acosta
当控件名称错误时,例如:TextBlock MyTextBlock = FindChild<TextBlock>(single_row, "WrongNameOfTextBlock"),会触发此异常。当传递错误的DataGrid时,例如NullDataGrid:DataGrid NullDataGrid = null; var row_list = GetDataGridRows(NullDataGrid)。请检查这些情况。 - Anatoliy Nikolaev
@AnatoliyNikolaev 但是如果你使用的是Page而不是Window呢? Page似乎没有ContentRendered事件。 - nam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接