CSharpFeeds - All your C# feeds in one place.

Sponsors

Saturday, June 09, 2007

On Demand Rendering in Windows Presentation Foundation

by Keyvan Nayyeri via Keyvan Nayyeri on 6/9/2007 6:10:22 PM

The lowest level of working with elements in WPF is applying on demand rendering techniques to customize the look and feel of an element when it's being rendered.  Using on demand rendering, you can change the way that an element is rendered in several ways.

On demand rendering is nothing more than overriding some methods from base classes in control hierarchy.  The most common method to override is OnRender().  But you can also override some other methods based on your needs.  For example, you can override OnMouseEnter() method if you want to deal with this event.

The core method in this technique is abovementioned OnRender() method which is defined by some control base classes and most of the elements that inherit from FrameworkElement can override it.  But when this method is called by WPF?  Whenever WPF needs to ask about the content that your element wants to display.  This means that it doesn't call this method many times (unlike what you saw in Win32).

OnRender() has a single parameter which is of DrawingContext type.  This parameter provides several low level methods to customize the rendering mechanism of an element.  These methods let you to draw text, shapes and other visuals in lowest level using brushes and pens.

Let me follow this discussion by giving an example.  I create a simple class and name it MyRender, then derive it from FrameworkElement base class.  In its implementation, I override OnRender() method from base class in the way that it uses DrawRectangle() and DrawText() methods from DrawingContext parameter to render a gray rectangle and a white text on it in the area specified by element.

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Media;

using System.Globalization;

 

namespace OnDemandRendering

{

    public class MyRender : FrameworkElement

    {

        protected override void OnRender(DrawingContext drawingContext)

        {

            drawingContext.DrawRectangle(Brushes.DarkGray, null, new Rect(0, 0, 200, 80));

 

            FormattedText text = new FormattedText("Keyvan Nayyeri", CultureInfo.CurrentUICulture,

                FlowDirection.LeftToRight, new Typeface("Tahoma"), 20, Brushes.White);

            drawingContext.DrawText(text, new Point(30, 25));

 

            base.OnRender(drawingContext);

        }

    }

}

Now I go back and forth and create a XAML file to use this element.  First step is to add a namespace mapping to my Window declarations and specify a prefix for it.  The second step is to declare this element in my XAML code where I want it to be shown.

<Window x:Class="OnDemandRendering.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:wf="clr-namespace:OnDemandRendering"

    Title="On Demand Rendering" Height="200" Width="300"

    >

  <Canvas>

    <wf:MyRender Canvas.Top="5" Canvas.Left="5" />

  </Canvas>

</Window>

At this stage, I can test my application and see the output.

Output

As you see, on demand rendering is a helpful technique to customize the way that WPF controls can be displayed.

Now playing: Richard Clayderman - The Rose

email it!bookmark it!digg it!

Original Post: On Demand Rendering in Windows Presentation Foundation

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us