FindVisualChild reference issue

12,618

Solution 1

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.

Solution 2

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

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>();
    }
}
Share:
12,618
user3812509
Author by

user3812509

Updated on June 06, 2022

Comments

  • user3812509
    user3812509 almost 2 years

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

    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 ?.