本文介绍了FindVisualChild 参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到并修改了以下代码,以便使用 iTextSharp 类将我的 dataGrid 导出到 pdf 文档.

I have found and modified following code in order to export my dataGrid to a pdf document using iTextSharp class.

private void ExportToPdf(DataGrid grid)
    {
        PdfPTable table = new PdfPTable(grid.Columns.Count);
        using (Document doc = new Document(iTextSharp.text.PageSize.A4))
        {
            using (PdfWriter writer = PdfWriter.GetInstance(doc, new System.IO.FileStream("Test.pdf", FileMode.Create)))
            {
                doc.Open();
                for (int j = 0; j < grid.Columns.Count; j++)
                {
                    table.AddCell(new Phrase(grid.Columns[j].Header.ToString()));
                }
                table.HeaderRows = 1;
                IEnumerable itemsSource = grid.ItemsSource as IEnumerable;
                if (itemsSource != null)
                {
                    foreach (var item in itemsSource)
                    {
                        DataGridRow row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                        if (row != null)
                        {
                            DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);
                            for (int i = 0; i < grid.Columns.Count; ++i)
                            {
                                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(i);
                                TextBlock txt = cell.Content as TextBlock;
                                if (txt != null)
                                {
                                    table.AddCell(new Phrase(txt.Text));
                                }
                            }
                        }
                    }
                    doc.Add(table);
                    doc.Close();
                }
            }
        }
    } 

问题出现在以下行:

DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);

Visual Studio 返回以下错误当前上下文中不存在名称‘FindVisualChild’".我如何添加这个参数?.

Visual Studio returns following error 'The name 'FindVisualChild' does not exist in the current context'. How do I add this parameter ?.

推荐答案

FindVisualChild 方法不是由 WPF 框架提供的,您必须添加它们.也许你想要这个:

FindVisualChild method is not provided by WPF framework, you have to add them. May be you want this:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
       where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

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

public static childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
{
    foreach (childItem child in FindVisualChildren<childItem>(obj))
    {
        return child;
    }

    return null;
}

在一些实用程序类中添加这些方法,以便它们可以重用.

Add these methods in some utility class so they can be reuse.

这篇关于FindVisualChild 参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 21:45