tags:

views:

754

answers:

2

Hi,

I noticed that when overriding the System.Windows.Window OnRender method fails to draw to the screen. I'm sure there's a good reason for this but was unable to find the explanation.

The following code demonstrates this:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        Pen pen = new Pen();

        public Window1()
        {
            InitializeComponent();
            pen.Brush = Brushes.Black;
        }

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
            dc.DrawRectangle(Brushes.Black, pen, new Rect(0, 0, 600, 400));
            dc.DrawEllipse(Brushes.Green, pen, new Point(300, 300), 50, 50);
        }
    }
}

replacing the drawing logic in a FrameworkElement (then setting the element as the content of a window) work fine.

thanks,

Danny

A: 

just as a check, does the snippet work with the dc.Draw... lines removed (i.e. is it the act of overriding or the additional draw commands that cause the issue)?

I found a link that suggests that simply re-ordering the calls may solve the issue (i.e. doing the dc.Draw... calls before calling base.OnRender(dc)).

http://www.codeproject.com/Messages/3356653/Override-OnRender-class-inherits-frameworkelement.aspx

Tom Carver
A: 

The answer is given by Chales Petzold here.

His answer is 'don't know why', however a comment correctly suggests that setting the window's Background to transparent fixes the bug.

DannyAsher