Wpf – Should I be using a Page, Window or UserControl

wpfxaml

I'm developing a new desktop application that will have several views such as a dashboard, event viewer, chart viewer to name a few. Essentially the user will switch between one of these view which will cover the whole screen and not just a part of it.

I'm currently stuck on whether I should be creating a new Window, Page or UserControl for each dashboard, event viewer, chart viewer etc.

I have done some reading and understand that Pages were built for navigation which in turn lets me keep a history of the navigation so I can go back/forward. However I don't think I need that functionality for my desktop application.

So can I use either a UserControl or a Window? Or should there only be one Window per application?

Thanks

Best Answer

A Window has things like Title bar (including min/max/close buttons, etc) and can be used to host XAML elements, such as User Controls.

You are certainly not restricted to using one Window per Application, but some applications would choose that pattern (one window, hosting a variety of UserControls).

When you create a new WPF Application, by default your app is configured (in App.xaml) like this:

<Application x:Class="WpfApplication1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

The StartupUri property tells the app which Window to open first (you can configure this if you wish)

If you would like to logically separate your Window into pieces and do not want too much XAML in one file, you could do something like this:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <local:HeaderUserControl Grid.Row="0" />
        <local:MainSectionUserControl Grid.Row="1" />
    </Grid>
</Window>

where HeaderUserControl and MainSectionUserControl are UserControls encapsulating the aspects of that Window, as needed.

If you want to show another Window, you can, in code, call Show or ShowDialog on an instance of the new Window you want to show...

Also - yes, a Page is part of a WPF Browser application, designed to be viewed in Internet Explorer.

Related Topic