Pavan 的个人资料the WPF way...日志列表 工具 帮助
2006/11/1

Don't forget the AdornerDecorator

While testing my new Custom Window control (which I should be blogging about soon) I ran into an interesting problem. The custom window control derives from System.Windows.Window and has its own ControlTemplate defined in its generic.xaml file. Now I have an application that makes use of Drag 'n' Drop (DnD) and was previously using the regular Window control. When I replaced that with my custom window control, the app would just crash as soon I started dragging some elements. Note that the same DnD code would work with the regular Window control.

Initially I suspected that there may be a bug in my DnD code, but the bug lied somewhere else. It was in the ControlTemplate for the custom window control. Huh?

The DnD code makes heavy use of Adorners and the AdornerLayer for showing the drag feedback. For the adorners to work, there has to be an AdornerLayer somewhere up in the Object Tree, since it acts as the rendering surface for the adorners. The API for retrieving an AdornerLayer is the static method

 

AdornerLayer.GetAdornerLayer(Visual elt)

The reason why this API would work for the regular Window and not for my custom window was because of the magic element called AdornerDecorator. For the Window control (which is a ContentControl), the ContentPresenter is surrounded by an AdornerDecorator, which ensures the success of the above API call, like so:

 

1 <ControlTemplate TargetType="{x:Type Window}"> 2 <Border BorderBrush="{TemplateBinding Border.BorderBrush}" 3 BorderThickness="{TemplateBinding Border.BorderThickness}" 4 Background="{TemplateBinding Panel.Background}"> 5 <Grid> 6 <AdornerDecorator> 7 <ContentPresenter ContentTemplate="{TemplateBinding 8 ContentControl.ContentTemplate}" 9 Content="{TemplateBinding ContentControl.Content}" /> 10 </AdornerDecorator> 11 ..... 12 ..... 13 </ControlTemplate>

The ControlTemplate that I defined did not have it and that used to crash the DnD code. So if you find yourself designing a ControlTemplate for a Window, make sure you Don't forget the AdornerDecorator ;)

 

Technorati tags: , , , ,

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。
PodilaPava​n 在此页禁用了评论功能。

引用通告

此日志的引用通告 URL 是:
http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!187.trak
引用此项的网络日志