Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Pages or Windows in WPF

Tags:

wpf

I'm new to using WPF. I have the following program I want to create: -Application opens up with one button -User clicks button and it takes them to a new page with various input.

I'm confused about how I need to do this. I tried opening a new window, but I don't want a window to open up, I want it to be all one Window. I tried creating a new page and navigating to it using NavigationService but couldn't get it to work.

Basically I want to create a workflow where the user enters some stuff, clicks the next button and is taken to a new page to enter some more information. Can anyone point me in the right direction?

like image 457
cmptrer Avatar asked Jun 16 '10 21:06

cmptrer


Video Answer


1 Answers

Use Pages in your application and use NavigationService to switch between them.

For example, if you have two pages in your paplication, "Page1" and "Page2" you can include the following in Page1.xaml:

<Button Content="Next" Click="NextClicked" />

and this in your Page1.xaml.cs:

void NextClicked(object sender, RoutedEventArgs e)
{
  NavigationService.Navigate(new Page2());
}

Alternatively you could use this:

  NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));

Generally it is easier to do the first, because you can also set properties of Page2. For example, if Page2 has a public "CurrentItem" property you could say:

  NavigationService.Navigate(new Page2 { CurrentItem = this.Something });

You can't do that with the Uri-based syntax.

You can also create instances of various pages (Page1, Page2, etc) and store them in your Application object, then switch to them like this:

  NavigationSerivce.Navigate(App.Page2);

This way if you ever navigate to Page2 later you will get exactly the same Page2 object. Alternatively you could use NavigationService's Journaling feature to help with this.

like image 80
Ray Burns Avatar answered Oct 04 '22 02:10

Ray Burns