Creating Custom Shapes with XAML
| In order to play around with XAML and WPF, you should probably pick up a copy of Visual Studio 2008. You can download the free Express Editions right here. |
Let's start out with an easy example to get warmed up. One of my favorite games is Tetris, so here's a Tetris shape.

Here's all the code required to make the above application.
<Window x:Class="XAMLCustomShapes.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre..."
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XAML Custom Shapes" Height="350" Width="350">
<Canvas>
<Path Canvas.Left="15" Canvas.Top="50"
Data="M 0,0 L 200,0 L 200,100 L 300,100
L 300,200 L 100,200 L 100,100 L 0,100 Z"
Stroke="Black">
<Path.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="0" Color="DarkBlue" />
<GradientStop Offset="1" Color="LightBlue" />
</LinearGradientBrush>
</Path.Fill>
</Path>
</Canvas>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre..."
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XAML Custom Shapes" Height="350" Width="350">
<Canvas>
<Path Canvas.Left="15" Canvas.Top="50"
Data="M 0,0 L 200,0 L 200,100 L 300,100
L 300,200 L 100,200 L 100,100 L 0,100 Z"
Stroke="Black">
<Path.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="0" Color="DarkBlue" />
<GradientStop Offset="1" Color="LightBlue" />
</LinearGradientBrush>
</Path.Fill>
</Path>
</Canvas>
</Window>
There's a lot of stuff here, but the only thing we care about is the Path object and its Data property. A Path has a lot of power when it comes to building custom shapes and that's what I'll be using today to draw them.
The shape is defined by the text in the
Data property. The syntax might look a little weird, but once you get used to it you'll find it to be a lot more efficient than previous methods. If you've drawn shapes with other languages, you might be familiar with the functions named something like lineTo, moveTo, arcTo, etc. WPF replaced those calls with one long string that contains special characters to represent each function. In my example, "L" refers to lineTo and "M" refers to moveTo. Below is an image that shows what each piece of the string represent what piece of the shape.

As you can see, I started my shape with an "M 0,0". This places my pen at the top-left corner of the shape. You can start the shape wherever you like, but (0,0) worked well for my Tetris block. The next piece, "L 200,0", draws a line from my starting point, (0,0), to the point specified after the "L", (200,0). When you draw a line the pen moves to the new point, so any subsequent calls will start from the last position. I simply continued using the "L" command to draw lines until my shape was complete. The last character in my data string, "Z", tells the path that the shape is finished.
There are a lot more commands other than "M", "L", and "Z". I'm not going to describe all of them since MSDN has a nice article on Path Markup Syntax that describes them in detail.
All right, I think we've got straight lines under control. Let's look a something a little more complicated - curves. WPF has lots of different types of curves - Quadratic Bezier, Smooth cubic Bezier, Smooth quadratic Bezier, and Elliptical Arc. Yep, I don't know what they are either, but the msdn article I mentioned in the last paragraph does an ok job of explaining them. If you really, really want to know more, check out the Wikipedia article on Bezier Curves.

Here's the same Tetris piece with some curves on the top and bottom. And the code:
<Window x:Class="XAMLCustomShapes.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre..."
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XAML Custom Shapes" Height="350" Width="350">
<Canvas>
<Path Canvas.Left="15" Canvas.Top="50"
Data="M 0,0 A 15,5 180 1 1 200,0 L 200,100 L 300,100
L 300,200 A 15,5 180 1 1 100,200 L 100,100 L 0,100 Z"
Stroke="Black">
<Path.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="0" Color="DarkBlue" />
<GradientStop Offset="1" Color="LightBlue" />
</LinearGradientBrush>
</Path.Fill>
</Path>
</Canvas>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre..."
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XAML Custom Shapes" Height="350" Width="350">
<Canvas>
<Path Canvas.Left="15" Canvas.Top="50"
Data="M 0,0 A 15,5 180 1 1 200,0 L 200,100 L 300,100
L 300,200 A 15,5 180 1 1 100,200 L 100,100 L 0,100 Z"
Stroke="Black">
<Path.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="0" Color="DarkBlue" />
<GradientStop Offset="1" Color="LightBlue" />
</LinearGradientBrush>
</Path.Fill>
</Path>
</Canvas>
</Window>
For the curves, I used an Elliptical Arc. Basically, all an Elliptical Arc does is draw a curve between the current point and the specified endpoint. The options for the arc are (in order in which they appear in the string): Arc Size (15, 5), Rotation Angle (180), IsLargeArc (1), SweepDirection (1), and the endpoint. The MSDN article on Elliptical Arc explains each one of those properties, so I won't repeat them here. As you can see, drawing curves is not that much different than drawing lines - there's just a few more options.
I think that just about does it for custom shapes. Using straight lines and the Elliptical Arc, you can make just about any imaginable shape. Of course, there are lots of different path options you should definitely explore to get your shapes exactly how you want them. Happy drawing.
Posted in WPF, XAML, All Tutorials by The Reddest |

January 12th, 2008 at 8:43 pm
I think this would be much more easier if you did this using Microsoft Expression Blend.Cause as far as designing custom shapes are concerned Expression Blend is a great option for anyone who wants to Design custom shapes without writing that much Xaml or procedural code to design better and save ofcourse save time.Anyway,best of luck for the future and happy coding.
January 14th, 2008 at 4:38 pm
I agree that Expression Blend is a much better option for drawing shapes. Blend is kind of expensive though, and I don’t have a copy. So whenever I want shapes, I have to draw them myself.
Also, I rarely like code generated from designers (anyone that’s used WinForms or Frontpage can attest to that). I don’t know the quality of Expression Blend XAML code though, so I can’t say whether or not it’s any good.
And lastly, knowing how shapes are created, even if it’s through Blend, is important when you want your code-behind to manipulate it at a later time.
January 15th, 2008 at 8:16 pm
Why not give it a try.Download Expression Blend it might be useful for you.Just for the testing purpose.It might be software of your liking.
January 15th, 2008 at 8:32 pm
I didn’t click that About Us link before posted those two comments.I’m extremely sorry to showing the brevity of suggesting you cause i’m not a man of that calibre what you people are.I’m niether a Computer Engineer nor have any background of scientefic education.I’m not a man of that kind of great ability.I live ina a small Country named Bangladesh.Live from hand to mouth.I just do something for nothing.Sorry guys for those two daring comments.
January 16th, 2008 at 9:39 am
Believe me, we don’t pretend to be the authority on programming. Any comments or suggestions you have are always welcome. I downloaded the trial of Expression Blend and glanced at it. I haven’t had a chance to really use it, but from what I saw, it looks like a very powerful application.
January 16th, 2008 at 2:32 pm
Real talents does not take pride of anything they are involved.They are never satisfied with the things they are doing.They always looking forward to do something great and i think you’re one of those people.I would like to mention one more thing that i really like this blog.