首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得DataGridTemplateColumn的底层(DataGridTemplateColumn)控件

如何获得DataGridTemplateColumn的底层(DataGridTemplateColumn)控件
EN

Stack Overflow用户
提问于 2019-05-30 22:36:32
回答 1查看 53关注 0票数 0

我使用这段代码将TextBox添加到DataGrid单元格中:(不,这里不能使用XAML )

代码语言:javascript
复制
Binding binding = new Binding("Fld_Company");
binding.Mode = BindingMode.OneWay;

FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(TextBox));
DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = frameworkElementFactory;
frameworkElementFactory.SetBinding(TextBox.TextProperty, binding);

DataGridTemplateColumn dataGridTemplateColumn = new DataGridTemplateColumn();
dataGridTemplateColumn.IsReadOnly = true;
dataGridTemplateColumn.Header = "Company";
dataGridTemplateColumn.CellTemplate = dataTemplate;

this.dataGrid.Columns.Add(dataGridTemplateColumn);

我有办法在没有XAML的情况下获得底层的TextBox控件吗?

我试过的是:

  • VisualTreeHelper,但GetChildrenCount()始终为0
  • FindName,但我还没有找到合适的FrameworkElement
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-22 08:37:50

在研究了DataGrid一段时间之后,我发现我的问题没有任何意义。我上面的代码只准备了DataGrid,但没有填充任何数据。在此之前,不会生成任何行,因此无法找到underlying TextBox controls

当DataGrid最终被数据填充时,获取底层控件的最佳方法似乎是捕捉LoadinRow事件。但是,当此事件触发时,行的加载仍未完成。需要临时分配第二个事件,在最后加载行时触发该事件。

代码语言:javascript
复制
{
    DataGrid.LoadingRow += DataGrid_LoadingRow;
}

private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    // The visual tree is not built until the row is "loaded". This event fires when this happend:
    e.Row.Loaded += DataGrid_Row_Loaded;
}

private void DataGrid_Row_Loaded(object sender, RoutedEventArgs e)
{
    DataGridRow dataGridRow = (DataGridRow)sender;
    // important: Remove the event again
    dataGridRow.Loaded -= DataGrid_Row_Loaded;

    NestedGridFieldProperty ngrProp = (NestedGridFieldProperty)dataGridRow.Item;

    // Get the "presenter", which contains the cells
    DataGridCellsPresenter presenter = coNeboTools.ConeboMisc.GetVisualChild<DataGridCellsPresenter>(dataGridRow);

    // Get the cells in the presenter
    var cells = GetVisualChildren<DataGridCell>(presenter);

    // Get the underlying TextBox in column 0
    TextBox underlyingTextBox = (TextBox)cells.ElementAt(0).Content;

    // the Item property of the row contains the row data
    var myData = dataGridRow.Item;

    // do what ever is needed with the TextBlock
    underlyingTextBox.Foreground = Brushes.Red;
}

    // Static helper method to handle the visual tree
    public static IEnumerable<T> GetVisualChildren<T>(DependencyObject dependencyObject)
           where T : DependencyObject
    {
        if (dependencyObject != null)
        {
            int childrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject);

            for (int i = 0; i < childrenCount; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in GetVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

    // Static helper method to handle the visual tree
    public static childItem GetVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
    {
        foreach (childItem child in GetVisualChildren<childItem>(obj))
        {
            return child;
        }

        return null;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56386471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档