Groups | Blog | Home
all groups > dotnet drawing api > february 2008 >

dotnet drawing api : WPF Background issue


Glenn
2/21/2008 11:48:04 AM
Hi,

I've created a control that derives from ScrollViewer and override the
OnRender method to draw onto the control. However, if the Background
property is set, the background always draws on top of whatever I draw.

Even if I don't call base.OnRender the background is drawn, so I'm not sure
what is drawing the background or why it would be drawn after OnRender is
Glenn
2/28/2008 10:49:08 AM
Here's some code to reproduce this problem:

Window1.xaml:

<Window x:Class="BackgroundIssue.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:BackgroundIssue"
Title="Background Issue" Height="375" Width="464"
xmlns:my="clr-namespace:System;assembly=mscorlib">
<Grid>
<test:CustomViewer x:Name="customViewer1" HorizontalAlignment="Left"
Width="238" Background="Red" />
</Grid>
</Window>


CustomWindow.cs:

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

namespace BackgroundIssue
{
public class CustomViewer : ScrollViewer
{
public CustomViewer()
{
this.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
}

protected override void OnRender(System.Windows.Media.DrawingContext
drawingContext)
{
drawingContext.DrawRectangle(null, new Pen(new
SolidColorBrush(Colors.Navy), 10), new Rect(20, 20, 400, 300));
}

}
}


This will create a window with the control on half of it. A rectangle is
drawn larger than the control so you can see that the red background is
covering what is drawn in the OnRender method.

-Glenn



[quoted text, click to view]
Bob Powell [MVP]
2/28/2008 6:38:40 PM
Post some code...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]
Glenn
2/29/2008 8:49:06 AM
I already switched to that design. I wasn't really expecting a magic fix,
just confirmation that I wasn't missing something.



[quoted text, click to view]
Bob Powell [MVP]
2/29/2008 5:27:49 PM
Rather than deriving from ScrollViewer create a new control to draw your
graphics and place it in the Content of a ScrollViewer.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
Bob Powell [MVP]
2/29/2008 10:43:41 PM
The WPF model is definitely one of composition. The Windows Forms model of
override and conquer is outmoded.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
AddThis Social Bookmark Button