Effectively writing datatemplates

Efficient way to use Data Templates: http://msdn.microsoft.com/en-us/magazine/dd483292.aspx

 

Background introduction:

 

Let’s have a look at what WPF have done in and after  this sample code: <control>.SetDataContext(<DataSource>)

 

First, this control will receive a PropretyChanged event, and then for each element in <datasource>, it will create a content present, and the ContentTemplate property is set. After all, this contentpresent is added to the control That’s all it has done in set data context.

 

The panel’s MeasureOverride method calls the measure method of each of its ContentPresenter children. If the ContentPresenter child has not yet created a visual tree to display its content, it must now do so. The content Presenter create this visual tree based on the template stored in its ContentTemplate property. The ContentPresenter must also set up the data binding between the properties of the element in the visual tree and the properties stored in its Contents property. The contentPresenter then calls the Measure method of the root element of the visual tree.

 

 

1.      Watch out Freezable Objects:

A children change will cause the entire object changes.

note that you should freeze any freezable object that will no longer be altered.

 

2.      Use ValueConverter

Data bindings can optionally reference little classes called value converters, which implement either the IValueConverter or IMultiValueConverter interface. Methods in the value converters named Convert and ConvertBack perform data conversion between the binding source and destination.

Normally, I would expect the converters to improve performance. I'm not sure why that's not the case here, but I wouldn't be surprised if it were a boxing and unboxing issue. In the DoublesToPointConverter, for example, the incoming double values have to be boxed and unboxed, and the outgoing Point has to be boxed and unboxed

 

3.      An intermediate presenter(To reduce the binding counts?

 

4.       Reduce elements:

Fast rendering slow response to change.

 

5.      Visual drawing

DrawingVisual is a lightweight drawing class that is used to render shapes, images, or text. This class is considered lightweight because it does not provide layout, input, focus, or event handling, which improves its performance. For this reason, drawings are ideal for backgrounds and clip art.

 

In order to use DrawingVisual objects, you need to create a host container for the objects. The host container object must be derived from the FrameworkElement class, which provides the layout and event handling support that the DrawingVisual class does not support. The host container object does not display any visual properties, since its main purpose is to contain child objects. For more information, see Using DrawingVisual Objects.

 

posted @ 2009-11-15 15:30  Pang pang Xiong  阅读(262)  评论(0编辑  收藏  举报