A better way to show different wpf pages in mainWindow? - wpf

I have several Wpf pages in my project, these pages have different forms and ui control indide theme. The image below shows the mainWindow and there are two button.
I want to show a specific page when I click on + or Edit button using the Frame control which is highlighted.
apparenatly this woks:
Page1 me = new Page1();
mainFrame.Content = me;
But it has an IE navigation sound and a toolbar appears after going to page2.
any better way to show diffrent pages and not using a frame?

You may want to convert the Page into a UserControl. You can then put that control inside some other container, such as a Grid. You'll have to manually swap out the pages in the container when navigating, but it looks like you're doing that anyway.

The purpose of the Frame control is to allow navigation. If you don't want navigation, then don't use Frame. You can turn off the navigation toolbar, but that won't actually disable navigation - there are mouse buttons and keyboard shortcuts for navigating back.
If you just want to host a UI element without navigation, use something simpler, like a Border element - put the content in its Child property. You can change the Child as many times as you like at runtime.

I was able to set the frame control's NavigationUIVisibility to Hidden. This solved the problem for me. I am using Visual Studio 2010 though so it might not be applicable to older VS versions.

Ian Griffiths, what you suggest increases the workload on the developer substantially. And you are stepping outside of the underlying paradigm of XAML.
In my case I'm developing a game application and have chosen WPF as the UI platform as much as possible. For me that means a intro screen, character select, etc. The purpose of Pages is to encapsulate the navigational need of such an application.
I suspect your downvote is due to your statement "If you don't want navigation...". Upon re-reading the original posters question I see he does want navigation, he just wants it on his own terms. I would have voted you down too. YotaXP's solution neglects the issues with using a User Control, particularly if it may contain other User Controls. It looks like Chris Calvert came up with an actual solution to the poster's issue within the parameters of the problem.
I would be curios if I could override the navigation hotkeys and such within the existing paragimn but that's properly in its own thread.

Related

How to use multiple views within a single view in WPF

I have a navigation bar in place that I have borrowed from the internet https://www.youtube.com/watch?v=YQ1EJJZBHyE
When a navigation button is clicked instead of navigating to a different page, I would instead like to have the gray area populated with its own View. I would also like each of these to have its own corresponding ViewModel as well.
I would prefer to roll this on my own, without another third-party library (Currently using the simple MVVMLight). However, if the solution is really involved a third-party library is fine - i'm not trying to reinvent the wheel.
I assume I would use something similar to a UserControl in WinForms, but i'm not sure how to handle the varying size when the left navpanel is expanded or retracted.
Here is a screen shot of the relevant code:
The answer with the most votes here very clearly illustrates a solution to what I was after, and fleshes out some of the details of the comments left here by the good gentleman assisting me.

WPF Pages Slide

I have an application that is made using WPF, i have multiple pages and a form that contains a frame, and i load the pages on a frame, and each page has back and next buttons.
what i want to do is to animate the navigation of the pages on the frame in a way when i press Next, the current page slides to the left and the following page slides also to the left and replacing the the current page, and when i press back the current page slides to the right and the previous page slides to the right replacing the current page.
how to do that?
I may be proven wrong here but I don't believe WPF exposes a way to implement this easily. There are methods for overriding existing transition animations in UWP.
For WPF you can handle this one of two ways...
Start the animation of the page sliding out. When the animation is
complete let it fire an event that will then call frame to change
pages.
Have the page offset by default and perform the animation to slide it
in to it's original spot.
or....
Capture the Navigating event of the frame, monitor NavigationMode, call the animations, make the navigation wait or mark e.Handled == true and recall it after with a flag that says it can continue.
Do the same for transitioning in.
Here's the problem; you won't have both pages on the screen at the same time and you'll have to write some fancy transitioning code to make all this work correctly. I would go as far as introducing custom controls with interfaces or DependencyProperties that have the transitioning functions embedded etc.
That said; I have had this problem in the past and to be fair I just gave up on the Frame control and pages and did it all manually. In the end it felt faster, easier to maintain, and I was able to achieve any effect I wanted.
So if you want my honest answer just don't use Frame and do it all manually... (This is speaking for WPF only. UWP has other features and more reasons to use the built in navigation than WPF does.)
Well, I believe the simplest solution would be to drop using Frames and implement your own navigation mechanism using something along the lines of the TransitioningContentControl (so you don't have to implement the transitions from scratch).
Basically, you'd have an list of your controls, an TransitionContentControl and two buttons (back and forward). So all you'd have to do is handle the clicks on the buttons and set the Transition accordingly (eg. when back button is pressed set it to Left and load the previous page).

How to make WPF page navigation code look better?

I want to make a program by WPF which includes several pages.
I have a Window and several Pages now.
In order to navigate, I use
this.Content = new Page1();
in the main window (from Window to Page), and
((Window)this.Parent).Content = new Page1();
between pages (from Page to Page), because Page can only be sub-element of Window or Frame.
However, the second line of code above looks quite ugly.
Is there a better way to achieve the same goal?
I have coded from several Windows Phone applications before, and I think it might be better to navigate between Pages rather than hide/show elements (such as Grids).
If your navigation code is on your Page class, either :
move it to the window class
create an event in the Page class, and react to it on the Window class.
Is there any reason why you can't just put a Frame in your Window ?
You could use NavigationService.
http://msdn.microsoft.com/en-us/library/ms750478.aspx
http://www.paulstovell.com/wpf-navigation
Maybe this could help:
http://azerdark.wordpress.com/2010/04/23/multi-page-application-in-wpf/

Rotating windows in WPF

I need to create a WPF application which is maximized and which rotates amongst about 10 different screens. Each screen will take the entire area and show different content.
I already know how to maximize the window with
My question is what is best to put inside that window to achieve what I want?
Ideally I'd be able to have 10 different .xaml files and I just load one after the other to take the entire screen. I'm not sure the best approach for accomplishing this in WPF.
Thank you!
One quick way to do this is to use WPF's built in page navigation. By making your root window a NavigationWindow and each view a class derived from Page (similar to work with to a UserControl or Window) you can just set the NavigationWindow.Source to a relative URI that points to the page you want to show (like a web browser) and simply switch it as needed.
This sounds like a classic MVVM application, which is simply too much to put into detail here. Google MVVM or Model-View-ViewModel, or pick up the book Advanced MVVM by Josh Smith (widely regarded as an expert in such things).
However, this is basically what you are going to have:
One class, the ViewModel, is an abstraction of the data that you need to bind to
Your data Model
A View for each thing you want to show. A View is simply something that holds your UI, be it a DataTemplate or a UserControl. Each View is bound to the ViewModel
The Views are the things that will "rotate" (although rotate in WPF implies animation and/or transformation). How you switch between them is up to you, although it sounds almost like something that would be done with a DispatcherTimer and animation (i.e. like fading between pictures in a slideshow).
This question is really too broad for this forum - you will need to do quite a bit of research on WPF fundamentals before proceeding. Again, MVVM is a good direction to start.
EDIT: Something More Lowbrow, per OP Request
This is probably as simple was you can make it (and still create separate XAML files for each piece of content):
First, create 10 UserControls (XAML files) for the stuff you want to show.
Next, add an instance of each of these user controls to your main window. Set the Visibility of each of these to Collapsed, except the first one to show.
Put a "Next" button on the main window.
In the code-behind, handle the Click event for the Next button. In there, keep track of which UserControl is visible, by name. Set the one that is currently visible to Visibility.Collapsed, and set the next one that is supposed to be visible to Visibility.Visible.
This is certainly an ugly solution, and not very WPF-ish, but it will get the job done.

How do you switch between "pages" of a Silverlight application?

I am currently loading the default file, page.xaml, but in that page, I am loading the content from another xaml file. With each "page" change, I just load the content from a different xaml file, and on and on.
Example: this.Content = new StartPage();
I'm running into some syntax issues, however, because of the way I am changing my content, and was wondering if there is a definitive answer on how to accomplish this?
For example, when trying to capture user's keystrokes, I would normally do:
this.Keydown += new KeyEventHandler(this_KeyDown);
but that event handler doesn't even fire in my situation. So, I'm looking for a new approach to my content-switching approach before revisiting the keystroke problem.
Have you looked at using Silverlight 3. It has a new Page Navigation functionality.Silverlight 3 Navigation
As far as content switching goes, I've always done what you propose in the question. Normally I create a MainPage.xaml which has has the frame of the application (usually a Grid for me). One of the cells in the Grid is considered the content area of the app. When the user takes an action that I would consider to be navigation, I create a new instance of a Page, which for me is a file like MyUserControl.xaml, and then add it to the appropriate content cell in the Grid. MainPage stays around for the life of the application and assists with navigation.
If you want something fancier, and want to take advantage of browser based back/forward buttons, you could look into the SL3 navigation like Correl suggested.
A Big problem with what your're doing is that journalization doesnt take place automatically when you swap out framework elements by creating them and plugging them in the codebehind. This means that you lose the back and forward functionality of the browser. You can manually journalize stuff when you swap out pages, but this is simply a hack to get your navigation approach working.
Take a look at Prism at www.compositewpf.codeplex.com/, specifically the MVVM method of GUI design, it'll save you alot of time later on. And remember, you dont need to go hardcore when you look at MVVM, u could always cut out alot of "dynamic" functionality if you're a one man band
Also swap to silverlight 3 and use the navigation application. If you cant, take a look at helix 0.3, it'll provide a more asp oriented approach to navigation. the link provides a really really good starting point, its a three part article, i suggest you read all three and download the sample application and understand it.
A book could have been written on your question, this has to suffice for now.

Resources