Re: WPF Background issue

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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



"Bob Powell [MVP]" wrote:

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.





Glenn wrote:
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
called. Does anyone have any ideas?

.



Relevant Pages

  • Re: Vb.net - Rotating a label (used to replicate a line)
    ... What I posted was a rhetorical question which did not require an answer and which was intended to indicate to you that even if you use a Line Control then SOMETHING MUST STILL DRAW THE LINE! ... Or do you think that because the line is not actually drawn by your code that it somehow magically appears without any resources or any drawing being involved? ...
    (microsoft.public.dotnet.languages.vb)
  • Drawline on Form at runtime with mouse?
    ... I would like to draw lines on the form at runtime. ... have the ability to control the line drawn to be Straight horzaontally, ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Problem drawing with GDI+ within a CStatic
    ... I want to draw lines on with GDI+. ... are not drawn if a checkbox is checked, ... sub-class the static to make my own control, ... If subclassing is not the right way to go, ...
    (microsoft.public.vc.mfc)
  • Re: Problem drawing with GDI+ within a CStatic
    ... needs to change it's states is actually the elegent way of doing things. ... You don't want to access the parent dialogs memebers from the child control. ... I want to draw lines on with GDI+. ... are not drawn if a checkbox is checked, ...
    (microsoft.public.vc.mfc)
  • RE: Image looses clarity in wpf control template
    ... I downloaded your sample project and run it on my machine. ... one drawn in the Image control. ... When we place a control into a parent control, ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.languages.csharp)