tags:

views:

362

answers:

2

I've created the simplest possible WPF control - It just overrides OnRender and draws a red rectangle.

However ONLY when setting the Background in XAML to Blue, the control is all blue with no red showing. If Background is not set, the red rectangle shows no problem.

How come the red rectangle is not displayed over the blue background when the background is set?

public class MyWpfControl : Control
{
    protected override void OnRender(DrawingContext drawingContext)
    {
        drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 100));
    }
}
+1  A: 

Because OnRender is done first, then the background is rendered - unless you're writing a very performance-intensive control, you do not need to override OnRender ever.

Check out Adam Nathan's book "WPF Unleashed" - it will get you started with the right way of writing controls and give a great introduction to WPF. Leave your Winforms knowledge at the door, things are very different, it's a separate way of thinking than the Winforms/Win32 approach.

Paul Betts
Cool thanks - Is there any place online that documents this behaviour? MSDN, articles, blogs, or somewhere in the WPF Unleashed book?If you can give me a link or page number I'll mark the answer correct. Not trying to be a jerk, but that is so counter-intuitive I need to verify it and will gladly look anywhere you say so.Thanks again -
ecard_guy
What more verification do you need, you've got the proof right there! OnRender gets called first, and then subsequently overwritten by the rendering for the background. OnRender is *very* low-level in WPF - it skips a lot of the layout and transformation magic that WPF provides and isn't meant to be used often
Paul Betts
I'll take your answer to mean that you strongly believe that's how it works, but do not know of any corroborating sources.Again the reason I'm skeptical is because if there were no way to use the Background property and OnRender at the same time that would seem to imply no one could ever use OnRender, and it's hard to believe none of the existing controls use it (Background works fine for them).
ecard_guy
Just wanted to add I do appreciate your response including the good book recommendation, thank you.
ecard_guy
Very few of the existing controls use it - using OnRender is saying "I intend to fully draw this control myself", you can't mix-and-match.
Paul Betts
+1  A: 

No sure how did you get the problem. Below is a simple app which takes use of MyWpfControl and it works fine. Hope it will be helpful even though the problem cannot be reproduced. Good luck.

namespace MyWpfControlDemo
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            WPFControl = new MyWpfControl();
            this.DataContext = this;
        }

        public MyWpfControl WPFControl { get; set; }
    }
}

The code in xaml.

<Window x:Class="MyWpfControlDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <UserControl Content="{Binding WPFControl}"/>
    </Grid>
</Window>
ping
Ping thank you for your reply, however my sample is a "WPF Custom Control", and your sample is a "WPF User Control", which is why it's not the same. Regards -
ecard_guy