none
How to force visuals to get rendered? RRS feed

  • Question

  • Hi,

    I´m programmatically creating shapes, like rectangles, and putting them in a canvas. I now want to be able to call functions like VisualTreeHelper.GetDrawing and VisualTreeHelper.GetContentBounds. The problem is that GetDrawing for my rectangle returns null, since it has not been rendered yet. I´m using VisualTreeHelper when I´m writing unit tests, so I somehow need to force WPF to render the visuals, without actually having the canvas rendered visibly.

    I´ve tried InvalidateVisual(), but I guess it just thinks that it doesn't have to be rendered so it does nothing. Is there a way around this?

    / Joel
    Monday, June 19, 2006 12:28 PM

Answers

  • There are a couple options that involve actually doing the rendering:

    1) Render to a RenderTargetBitmap.  This can also let you test at various DPI settings and might prove an interesting test environment.

    2) Actually render to a window during the test and perform the test by attaching to the Loaded event of the window under test.

     

    Monday, June 19, 2006 2:48 PM
    Moderator

All replies

  • try something like this (c1 is a Canvas)

    Rectangle rect = new Rectangle();

    rect.Height=200;

    rect.Width=200;

    rect.Fill=Brushes.Aqua;

    rect.SetValue(Canvas.LeftProperty,20.0);

    rect.SetValue(Canvas.TopProperty,20.0);

    rect.Name = "MyRect";

    c1.Children.Add(rect);

    this.RegisterName(rect.Name, rect);

    c1.UpdateLayout();

    DrawingGroup group1 = VisualTreeHelper.GetDrawing(FindName("MyRect") as Visual);

    Monday, June 19, 2006 1:00 PM
    Moderator
  • What should I call RegisterName on? I don't have this.RegisterName since I´m in a TestFixture class.

    I´m not sure what this would accomplish?

    My original problem is that I want to write NUnit tests, testing code that uses the VisualTreeHelper.GetDrawing for example. GetDrawing returns null though, since no rendering has actually been performed, due to no visual rendering in my unit tests. I need a way to force rendering of visuals, so that the VisualTreeHelper can return the drawing and bounds.

    I rather not have "special" code lika RegisterName for this, whatever it does, I'm not sure it will help either?!

    Thanks for the answer...

    / Joel
    Monday, June 19, 2006 1:22 PM
  • UpdateLayout makes the measure and arrange valid of the rectangle in the sample and registername registers the name so we can use FindName.

    how do you plan on getting all the things  added to the canvas to call VisualTreeHelper.GetDrawing method

    Monday, June 19, 2006 1:28 PM
    Moderator
  • Here is my code sample, I added UpdateLayout but still teh bound returned is empty and the drawinggroup is null.

    [TestFixtureSetUp]
    public void FixtureSetUp()
    {
        m_Canvas = new MyCanvas();
        m_MasterRectangle = new Rectangle();
        m_MasterRectangle.Width = 400;
        m_MasterRectangle.Height = 200;
                
        m_Canvas.Children.Add(m_MasterRectangle);
        m_Canvas.UpdateLayout();
    }

    [Test]
    public void WidthWhenRotated()
    {
        m_MasterRectangle.RenderTransformOrigin = new Point(0.5, 0.5);
        RotateTransform rotateTransform = new RotateTransform(45);
        m_MasterRectangle.RenderTransform = rotateTransform;

        m_Canvas.UpdateLayout();
                      
        Rect rect = VisualTreeHelper.GetContentBounds(m_MasterRectangle);
        DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(m_MasterRectangle);
    }

    / Joel
    Monday, June 19, 2006 1:44 PM
  • There are a couple options that involve actually doing the rendering:

    1) Render to a RenderTargetBitmap.  This can also let you test at various DPI settings and might prove an interesting test environment.

    2) Actually render to a window during the test and perform the test by attaching to the Loaded event of the window under test.

     

    Monday, June 19, 2006 2:48 PM
    Moderator
  • I'm trying to render a UIElement to a bitmap using RenderTargetBitmap and it gives me a blank bitmap unless I first display it on the screen to force it to "render" the visual to the screen.  Any other tips?
    Wednesday, May 9, 2007 11:48 PM