编程方式复制WPF控件 [英] Copy a WPF control programmatically

查看:292
本文介绍了编程方式复制WPF控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选项卡控件,当用户想要添加到它,那么我想复制一对夫妇已经存在(不只是引用它们)的元素。现在,到目前为止,我刚刚硬拷贝我想要的变量。但我已经走在了自动调整大小代码 - 也就是复制的元素显着原来的背后调整窗口滞后时栽跟头。此外,它只是不可能让每一个拷贝,我需要复制该名单的增长元素。有没有一些方法,我可以使用,这将复制一个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.

推荐答案

我可能被误理解你的问题,但你可以创建一个自定义用户控件,每当你需要添加一个新的控件,只需创建该控件的新实例,并将其添加到您的场景,这样你可以使用的DataContext s到帮助进行数据绑定,您可以从控制使用您的复制:

I may be miss-understanding your question, but you could create a custom UserControl, and whenever you need to add a new control, just create a new instance of that control and add it to your scene, this way you can use DataContext's to help with the data binding which you can use from the control your copying:

MyControl newControl = new MyControl { DataContext = controlToCopy.DataContext };
myGrid.Children.Add(newControl);

或类似...

或你需要它是比这更动态的?

or do you need it to be more dynamic than that?

这篇关于编程方式复制WPF控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆