How can I get the Drawing of a Button for rendering on an Adorner?

I have a control, for example a Button, which i create in code and don't (!) want to display in the window directly. Now I need the Drawing of the Button to paint it on an Adorner, because, as I see, the DrawingContext only supports DrawDrawing(..). How can I get the Drawing of a Button because the following sample just doesn't work since I never get a DrawingGroup back from the VisualTreeHelper.GetDrawing(foo) function:

//my adorner OnRender method:

protected override void OnRender(DrawingContext drawingContext)
{

    Button foo = new Button();
    foo.Content = "bla";

    DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(foo);

    if (drawingGroup != null)
    {
        Debug.WriteLine("got group!");
        // Enumerate the drawings in the drawing group.
        for (int i = 0; i < drawingGroup.Children.Count; i++)
        {
            drawingContext.DrawDrawing(drawingGroup.ChildrenIdea as Drawing);
        }
    }
}

Can anybody help me out on this one Am I missing something



Answer this question

How can I get the Drawing of a Button for rendering on an Adorner?

  • elixic

    Thanks for your fast reply! Will check tomorrow if your tip suits my needs!

    Basically I want to replicate the behaviour of the WPF Validation where the content of the Validation.ErrorTemplate control template gets rendered to an adorner that is shown at any control for error information. Unfortunately we can't use the WPF Validation in our programs as we do validations in our business logic to be more UI independent so I need to replicate the error indication behaviour by myself but until now i have no clue (as you saw with my trys) how to do this.

    Maybe you know the magic behind the Validation error indication stuff using adorners in WPF



  • guyinkalamazoo2

    Paul Stovell has written an excellent article on codeproject discussing that very topic.

  • Covi

    His article also describes a customized validation tool that works similar to IErrorProvider in winforms. The validation can be hooked into a business rules engine.

  • Charlotte Adam

    ivolved_Mike_Brown wrote:
    Paul Stovell has written an excellent article on codeproject discussing that very topic.

    Well, his article (which i had known) has not the solution for my problem as it uses the Validation Class of WPF but I can't use this class for reasons described above, so I need to plumb the Adorner stuff in a similar manner by myself but i don't know how it is done.


  • aliasx

    That's what I would have guessed.

  • Blake01

    I believe this doesn't work because Button uses styling to be rendered and doesn't have its own OnRender method. Note that in general getting the drawing data from a visual is going to be a fragile affair because you won't get the drawings of any of the visual's children. Many classes that are exposed as a single control are in fact the amalgomation of a number of child visuals.

    Instead of trying to get the drawing out of a Button, can you instead use a VisualBrush

    protected override void OnRender(DrawingContext drawingContext)
    {
    Button foo = new Button();
    foo.Content = "bla";

    foo.Width = 100;
    foo.Height = 50;

    VisualBrush brush = new VisualBrush();

    brush.Visual = foo;

    drawingContext.DrawRectangle(
    brush,
    null, /* no pen */
    new Rect(0 /* x */,0 /* y */,100 /* width */,50 /* height */));
    }



  • JUANCARLOSR

     ivolved_Mike_Brown wrote:
    His article also describes a customized validation tool that works similar to IErrorProvider in winforms. The validation can be hooked into a business rules engine.

    I know the content of the article very well since I use the system described in it with validation in the business layer, but Paul does NOT describe how to render error messages to an adorner in a way the WPF Validation class does.

    EDIT: I think i got the solution, will post later if it works :)

    SOLUTION FOUND, thanks to the snoop program by pete blois.

  • How can I get the Drawing of a Button for rendering on an Adorner?