Creating a lightweight custom element
Custom controls provide a way to create a user interface based on XAML elements. Sometimes, however, a more complex UI is required, that cannot be easily achieved by XAML alone, or where low level control is desired. In such cases, we can create a lightweight element, deriving from FrameworkElement
that does its own drawing without creating any particular element. This makes the control consume less memory and WPF layout logic does not need to consider many elements.
In this recipe, we'll take a look at the way to implement such lightweight elements.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a simple bar graph control to demonstrate the ability to draw in a more "manual" fashion.
Create a new WPF application project named
CH10.CustomRendering
.Add a new class named
BarGraph
that derives fromFrameworkElement
. This class will hold an array of values to show as a bar graph.Add a dependency property named
Values
to the...