且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在WPF c#中的MDI控件中单击按钮时从另一个用户控件打开用户控件

更新时间:2021-10-12 08:21:08

控件不应该这样做,除非新控件完全在控件中打开它。

通常情况并非如此:两个控件都在一个表单中,并且不应该知道彼此的存在。在这种情况下,带按钮的控件(称为Child1)表示表单处理的事件,导致表单创建一个新控件(称为Child2)并将其添加到Controls集合。



这比听起来简单:在两者之间传递信息两种形式,第3部分:儿童到儿童 [ ^ ]几乎涵盖了表单,但是表单中的控件的原则(甚至代码)是相同的。
The control should not do it, unless the new control is fully within the control opening it.
Normally, this is not the case: both controls are within a form, and should not know of the existence of each other. In this case, the control with the button (call it Child1) signals an event which the form handles, causing the form to create a new control (call it Child2) and add it to the Controls collection.

It's simpler than it sounds: Transferring information between two forms, Part 3: Child to Child[^] covers it pretty much for forms, but the principles (and even the code) is the same for controls within a form.