Creating basic XAML and Windows Presentation Foundation (WPF) 2D Graphics is very easy and is similar to the SVG Syntax. The following sample shows a line, an ellipse, and a rectangle. Gradients and text could easily be added to these objects as well.
Code for the example is shown in Listing 1.
Listing 1: Simple 2D Graphic Example
<Rectangle Fill="#FF0000FF" Height="72" Canvas.Left="74"
Stroke="#FF000000" Canvas.Top="77" Width="131" />
<Ellipse CenterX="231" CenterY="76" Fill="#FFFF0000" Canvas.Left="0"
RadiusX="55" RadiusY="56" Stroke="#FFA52A2A" StrokeThickness="5" Canvas.Top="0" />
<Line Canvas.Left="0" Stroke="#FF008000" StrokeThickness="10" Canvas.Top="0"
X1="318" X2="96" Y1="46" Y2="211" />
If you are building UIs that require common controls such as List Box, Toggle, Check Box, and Calendar, WFP has all these and other common WinForm controls. It is also easy to add and modify controls to meet your personal needs.
A key differentiator in using WPF is the dependency property system. It is very important that developers understand dependency properties based on the features they offer and ensuring that they are leveraged. In WPF both regular and attached dependency properties are used to set the attributes of an object. The easiest way to establish and create a dependency property is using XAML. Dependency properties can be used for elements such as Data Binding, Animation, and Styling. Properties can also be created easily and directly through WPF. The developer really does not have to be concerned with anything else that may be hiding.
Most public properties on WPF objects have a corresponding static dependency property. The dependency property is used as a key in locating the value of the object you are looking for. Below is what a dependency property looks like in the dependency property system in WPF.
Listing 2 Dependency Property System in WPF
public class DependencyObject
public static DependencyProperty dependencyProperty1 =
DependencyProperty.Register("Property1", typeof(Int), typeof(Button));
public static DependencyProperty dependencyProperty2 =
DependencyProperty.Register("Proprty2", typeof(String), typeof(Button));
System.Collections.Hashtable hashTable = new
public object GetValue(DependencyProperty dp)
public void SetValue(DependencyProperty dp, object value)
return hashTable.Add(dp, value);
Unlike regular dependency properties, ‘attached’ dependency properties are static dependency properties that belong to one class but can be used in another. Below is a XAML code example.
<Rectangle Fill="#FF0101FF" Height="61" Canvas.Left="65" Stroke="#FF000333"
Canvas.Top="33" Width="142" />
Regular dependency properties of the Rectangle include Fill, Height, and Stroke. The Canvas.Top property is an attached dependency property. This property belongs to the Canvas class, but is used by the Rectangle class to specify its X and Y position within a canvas. These properties work well with Canvas, Grid, and DockPanel classes in WPF.