FindVisualChild reference issue

前端 未结 2 724
既然无缘
既然无缘 2020-12-03 19:40

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)
           


        
2条回答
  •  鱼传尺愫
    2020-12-03 20:17

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

    public static IEnumerable FindVisualChildren(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(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }
    
    public static childItem FindVisualChild(DependencyObject obj)
        where childItem : DependencyObject
    {
        foreach (childItem child in FindVisualChildren(obj))
        {
            return child;
        }
    
        return null;
    }
    

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

提交回复
热议问题