How to copyclone a UIElement and keep the layout/render information?
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 a开发者_StackOverflownother 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
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.
精彩评论