1

I've been able to create a WriteableBitmap from controls that are rendered in my Silverlight application UI and export them to an XImage via SilverPDF and include them in a PDF document. This conversion and export process works fine.

Now, I'm working on a piece where I have a DataGrid that I'm building completely in the code behind and I do not intend to render it on the UI. My problem now is getting a WriteableBitmap of the DataGrid so that I can run it through the above process to convert it to an XImage and export it to a PDF document.

I've seen this thread which seems to also describe my situation, but it did not help: WPF - Get size of UIElement in Memory?

I'm wondering if the Measure and Arrange functions only apply on the WPF side and not Silverlight.

How can I get a WriteableBitmap from an un-rendered control? Here is the relevant code so far:

//Add DataGrid with feature's attributes.
        DataGrid dg = new DataGrid();
        dg.AutoGenerateColumns = false;
        dg.HeadersVisibility = DataGridHeadersVisibility.None;
        DataGridTextColumn keyColumn = new DataGridTextColumn
        {
            Binding = new Binding {Path = new PropertyPath("Key")}
        };
        DataGridTextColumn valueColumn = new DataGridTextColumn
        {
            Binding = new Binding {Path = new PropertyPath("Value")}
        };

        dg.Columns.Add(keyColumn);
        dg.Columns.Add(valueColumn);
        dg.ItemsSource = _featureToPrint.Attributes;

        sp.Children.Add(dg);

        return sp;
    }

Once that sp (stackpanel) is returned, I call the Measure / Arrange method on it:

private static void ArrangeElement(UIElement element, double height, double width)
    {
        var box = new Viewbox { Child = element };

        box.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        box.Arrange(new Rect(10, 10, width, height));
    }

Any help or explanation of what I'm missing would be greatly appreciated! Thanks!

Community
  • 1
  • 1
lg1382
  • 71
  • 11

0 Answers0