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.

 

转载于:https://www.cnblogs.com/pangpangxiong/archive/2009/11/15/1603364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值