How to copyclone a UIElement and keep the layout/render information?

别说谁变了你拦得住时间么 提交于 2019-12-11 04:16:53

问题


I wish to copy a complex data-bound UIElement but keep the Binding, Layout and Rendering information from the original UIElement.

Creating a new UIElement seems inefficient since I would have to do another complete Binding/Measure/Arrange/Render pass.

The closest I've got so far is to create a new DrawingVisual, open it for rendering, and DrawDrawing using the source UIElement DrawingGroup.

DrawingVisual dv = new DrawingVisual();
DrawingGroup dg = VisualTreeHelper.GetDrawing(SourceElement);

using (DrawingContext dc = dv.RenderOpen())
{
     Drawing d = dg.Children[0];
     dc.DrawDrawing(d);
}

return dv;

Is this a good approach and will it work for complex UIElements (e.g. lots of Panels within Borders, custom Controls etc etc)?

To give you a context, I am making a DocumentPaginator which is working well, but inefficeintly due to the recreation of identical UIElements.

Thanks in advance, Pete


回答1:


If it's only for presentation purposes, you can use a VisualBrush. More information can be found here, here, and here. It even reflects any live updates made to the attached visual.



来源:https://stackoverflow.com/questions/5890298/how-to-copyclone-a-uielement-and-keep-the-layout-render-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!