wpf FindVisualChild 参考问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25229503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:19:03  来源:igfitidea点击:

FindVisualChild reference issue

c#wpfdatagrid

提问by user3812509

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

我找到并修改了以下代码,以便使用 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();
                }
            }
        }
    } 

The problem occurs in the following line:

问题出现在以下行中:

DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);

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

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

回答by Rohit Vats

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

FindVisualChildWPF 框架不提供方法,您必须添加它们。可能你想要这个:

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.

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

回答by Shocked

Also a common practice is to use these methods (posted by Rohit Vats) as extension methods, like this:

另外一种常见的做法是使用这些方法(由 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
    {
        ...
    }

}

And then in your code:

然后在你的代码中:

using Utils;

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