Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a WPF control programmatically

Tags:

c#

wpf

I've got a tab control, and when the user wants to add to it, then I want to copy a couple of elements that already exist (not just reference them). Now, so far I've just hard-copied the variables I want. But I've come a cropper in the automatic sizing code- that is, the copied element noticeably lags behind the original when resizing the window. In addition, it's just infeasible to keep copying each element that I need to copy as that list grows. Is there some method I can use that will copy a WPF control? Right now, that's just a text box and a tab item.

like image 649
Puppy Avatar asked Jul 07 '10 01:07

Puppy


1 Answers

I can't quite tell what it is you're trying to do but if you want a new instance identical to an existing control instance you can use XamlWriter and XamlReader to serialize/deserialize the control:

MyControl copy = XamlReader.Parse(XamlWriter.Save(controlInstance)) as MyControl;
like image 66
John Bowen Avatar answered Sep 20 '22 18:09

John Bowen