WPF: How to implement a generic window?
I'd like to have a reusable window in my desktop application that coul开发者_StackOverflowd 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
A simple approach would be a Window with a Frame, and then load pages into that Frame on demand.
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.
The DataWindow of Catel does exactly this. You can find the source code here:
DataWindow.cs
You should seriously consider using Prism (developed by Microsoft, coming out of the Patterns & Practices group) for this; it was built to handle exactly these scenarios, providing tools for composing UIs.
I ended up getting some tips and tricks from this article:
http://www.codeproject.com/KB/WPF/CustomFrames.aspx
it was exactly what i was looking for!
Is this what you are looking for?
http://jamescrisp.org/2008/05/26/wpf-control-inheritance-with-generics/
精彩评论