Custom Shape
Written by Administrator   
Wednesday, 07 April 2010
Article Index
Custom Shape
Optimising DefiningGeometry
A custom shape example
Repositioning a shape
Choosing a method

Best practice?

So which method should we use?

It all depends. Recreating the geometry is easy and it isn't too inefficient if the geometry is quick to generate. There is a theoretical satisfaction in applying a manual shift to the co-ordinates of the stored geometry but in a general case it could become complex. Setting the geometries transform seems like overkill but it is extremely general - you can implement scaling or rotation in the same way. Given that hardware accelerated graphics can apply transformations efficiently this seems like the best general method.

Banner

Notice however that all of the examples in this article duck the issue of implementing the Shape properties as dependency properties.

There is one last big problem with the custom Shape class as it stands. Currently there is only a constructor with parameters and this means that we cannot instantiate a Tree using XAML as XAML needs a class to have a parameterless constructor.

We could just add a parameterless constructor and then allow the properties to be set after the creation of the instance. However, while it is reasonable to allow positional and even scale parameters to be set after the instance has been created, there is a sense in which L, s, t and dt determine the type of tree and these should be set when the tree is created.

There is also the problem that you can't generate a tree until all of these parameters have values. One easy way around the problem is to simply give each one a default value and generate the geometry each time one of them is set. This approach is fine if you are happy with regenerating the geometry each time a parameter is changed. However, if you want to generate the geometry once when the instance is created and then apply transformations to move, scale and rotate then it is more difficult to allow a parameterless constructor.

The solution is to use the ISupportInitilize interface and a batch parameter initialisation approach. This puts off the creation of the geometry until everything is defined and its honoured by XAML.

Coming soon - parameterless constructors with the help of ISupportInitialize.

Follow us on Twitter  to find out when this article is published.

Banner


Bitmap Effects

WPF bitmap effects are easy to use but subtle. Before moving on to consider custom effects we take a careful look at what is provided as standard.



Drawing Bitmaps – DrawingImage and DrawingVisual

 

WPF provides multiple ways to convert vector drawings to bitmaps. Find out how DrawingImage and DrawingVisual work and when to use which. On the way we look at how to create 2D vector drawings.



Custom BitmapSource

Even if you don't anticipate implementing your own custom BitmapSource finding out how to reveals quite a lot about the way that WPF bitmaps work.



Custom Bitmap Effects - HLSL

In the article Custom Bitmap Effects - Getting started we discovered how to work with HLSL in WPF. Now we are in a position to write more sophisticated shaders and  this means learning some more  [ ... ]



Using the WPF .NET 4.0 DataGrid

WPF DataGrid (.NET 4)  can be difficult to understand if you aren't used to thinking about objects and collections. This easy to follow introduction explains where the rows and columns have gone. [ ... ]


Other Articles

<ASIN:1590599551>

<ASIN:0596159838>

<ASIN:0470596902>

<ASIN:0470563486>



Last Updated ( Wednesday, 07 April 2010 )