FindVisualChild引用问题

本文关键字:问题 引用 FindVisualChild | 更新日期: 2023-09-27 18:17:24

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

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'不存在于当前上下文中'。如何添加该参数?

FindVisualChild引用问题

FindVisualChild 方法是WPF框架没有提供的,必须自己添加。也许你想要这个:

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

将这些方法添加到一些实用程序类中,以便可以重用。

也是一种常见的做法是使用这些方法(由Rohit Vats发布)作为扩展方法,像这样:

static class Utils
{
    public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj)
           where T : DependencyObject
    {
        ...
    }
    public static childItem FindVisualChild<childItem>(this DependencyObject obj)
        where childItem : DependencyObject
    {
        ...
    }
}

然后在你的代码中:

using Utils;
class MyCode
{
    public static DataGridCellsPresenter GetPresenter(DataGridRow row)
    {
        return row.FindVisualChild<DataGridCellsPresenter>();
    }
}