Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: How to implement a generic window?

I'd like to have a reusable window in my desktop application that could potentially have different xamls "injected" into it. Because I'd like all my windows to have the same look (I have a nifty title bar and close button, etc)

For example, I'd like to have a messagebox (ok/cancel/string) used in it. I'd like it to be used to host a form, etc.

I currently have:
MyWindow.xaml/cs (is a Window)
MyMessageBox.xaml/cs (is a User Control)
MyForm.xaml/cs (is a User Control)

So what is the best way to implement this. Do I just put a ContentPresenter in MyWindow and then somehow "inject" the other xamls into it? Do I need the other controls to inherit something from the base window?

I'm just looking for some feedback on the best way to go about what I'm trying to do. Thanks

like image 762
Shai UI Avatar asked Jul 01 '26 21:07

Shai UI


2 Answers

A simple approach would be a Window with a Frame, and then load pages into that Frame on demand.

like image 120
Henk Holterman Avatar answered Jul 04 '26 11:07

Henk Holterman


I agree with with Henk Holterman's answer. I've just written a small 'framework' for WPF applications of this sort. I use an inteface on the 'content' pages to define common functionality for the code-behind; I have a generic page template that I use to give me a caption block, close button, etc.

I'd avoid visual inheritance with WPF unless you absolutely need it - it can be rather tricky to get right.

like image 29
Justin Denton Avatar answered Jul 04 '26 09:07

Justin Denton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!