I am currently writing a desktop application, but I cannot seem to get my head around what to use when redirecting someone to a new section of the application.
My options appear to be
- Window
- Page
- UserControl
but I don't understand what the difference between them is, and when I should use each one.
Could someone explain the differences for me, and give an example of what situations/applications you may use each one for?
Best Answer
A Window object is just what it sounds like: its a new
Window
for your application. You should use it when you want to pop up an entirely new window. I don't often use more than oneWindow
in WPF because I prefer to put dynamic content in my main Window that changes based on user action.A Page is a page inside your Window. It is mostly used for web-based systems like an XBAP, where you have a single browser window and different pages can be hosted in that window. It can also be used in Navigation Applications like sellmeadog said.
A UserControl is a reusable user-created control that you can add to your UI the same way you would add any other control. Usually I create a
UserControl
when I want to build in some custom functionality (for example, aCalendarControl
), or when I have a large amount of related XAML code, such as aView
when using the MVVM design pattern.When navigating between windows, you could simply create a new
Window
object and show itbut like I said at the beginning of this answer, I prefer not to manage multiple windows if possible.
My preferred method of navigation is to create some dynamic content area using a
ContentControl
, and populate that with aUserControl
containing whatever the current view is.and in your navigate event you can simply set it using
But if you're working with WPF, I'd highly recommend the MVVM design pattern. I have a very basic example on my blog that illustrates how you'd navigate using MVVM, using this pattern: