开发者

WPF custom control question

i've done this before but i cannot find my old code.

how do you embed a window inside a window.

let say i created a custom form and saved it as Window1.xaml, and want to embed it in Window2.xaml, without copy and pasting the xaml code.. TIA

EDIT: i think my question is somewhat misleading, i'll rephrase it.

i have this Window1.xaml i added custom headers and background images/colors.

then in Window2.xaml, i want Window1 to be a custom control and embed it here.

no开发者_C百科t sure if its Content Presenters, still googling for the answer :)


You can't host a WPF Window inside another WPF Window, but you could move the content from one Window to another:

var window1 = new Window1();
var window2 = new Window2();

var content = window1.Content;
window1.Content = null;
window2.Content = content;

Note that you set window1.Content to null or else you get an exception, since the content will have a visual parent otherwise.

UPDATE It appears all you need to do is to copy all the XAML between the <Window></Window> tags in Window1 into a new UserControl, then host that user control in Window2.


I believe you should make use of Pages or usercontrols in such cases. This way you can navigate to other parts/pages/controls defined in application. CodeKaizen is right , you can't host a window inside another window


I'm not sure you can do that - however, you shouldn't put the user interface directly into a window, use a normal control (either custom or user) instead and reuse that in your windows.


I know you can do it in code behind

//Window you want to show
Window1 child = new Window1(); 

object content = child.Content;
child.Content = null;

//Where to show
this.grid1.Children.Clear();    
this.grid1.Children.Add((UIElement)content);

Hope helps!


It sounds like you really want a UserControl. Change Window1's type from Window to UserControl and then put that UserControl in Window2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜