2

I am totally new to WPF, I mainly use drag&drop, rarely the XAML code itself. In my previous WPF apps, I just used one or two windows.

In a course it is a requirement to build a small app associated with a database.

In my app, there will be a main window and 5 more windows. Is it possible to use WPF pages instead of WPF windows in the same way as windows?

How to navigate back to the main window?

I have googled a lot, but I didn't find any solutions for my problems so far.

 private void BackFromNewCostPage_Click(object sender, RoutedEventArgs e)
        {
            MainWindow main = new MainWindow();
            this.NavigationService.Navigate(main);
        } 

I got a NullReferenceException here.

BonEd
  • 37
  • 5
  • 1
    Why use WPF when you still program the gui like winforms? why not use winforms then? also look into https://learn.microsoft.com/de-de/dotnet/api/system.windows.controls.frame?view=netframework-4.8 – Denis Schaf Oct 17 '19 at 09:27
  • 2
    To add to @DenisSchaf: If you are required to use WPF, then do not create your application in the winforms way, but try to wrap your head around concepts of WPF and apply them instead. – Max Play Oct 17 '19 at 09:32
  • How should I use WPF? – BonEd Oct 17 '19 at 09:32
  • i recommend googleing "WPF Tutorial" of something comparable. Wpf is comparable to HTML or any other markup language. Doing something is easy. doing something right requires some research and learning – Denis Schaf Oct 17 '19 at 09:44
  • What is the difference between drag&drop a controll and then set the appearance and the events for it from XAML and write everything in XAML? It is very slow and time consuming. Man should know so much by heart, and programming itself is still very hard for me! – BonEd Oct 17 '19 at 09:46
  • 2
    drag n Drop creates random XAML with margins and absolute positioning you wont be able to use the advantages of MVVM and your gui will never be scalable to multible devices – Denis Schaf Oct 17 '19 at 09:47
  • Event handlers in `WPF` are last resort! One would use a Command with Binding on a view model to achieve desired behaviour. MvvM will help you keep the app consistent and structured, unlike WinForms which is basically free for all (anything can do everything). 2 up votes makes me worry about the audience that reads this. Like @DenisSchaf said designer will make everything stay in one position where you dropped stuff, so all Grids and StackPanels will be useless. And forget about naming buttons and **DON'T** use labels unless you have to, even then reconsider. – XAMlMAX Oct 17 '19 at 10:01
  • Unfortunately, I don't understand anything from this. We have to make that app to finish the course. We do not learn enough because the course is too short and we go slowly. Man must learn so many thing from the internet alone. My English knowledge is too poor .... – BonEd Oct 17 '19 at 10:10
  • The approach I would recommend is called viewmodel first ( you can google it ). Bind content of a contentcontrol to a property in a viewmodel. Use a datatemplate associated with the type of a viewmodel to template that out into UI. https://social.technet.microsoft.com/wiki/contents/articles/52485.wpf-tips-and-tricks-using-contentcontrol-instead-of-frame-and-page-for-navigation.aspx – Andy Oct 17 '19 at 11:30
  • @DenisSchaf it makes absolutely no difference whether you set the margins using drag and drop or type them manually, numbers are numbers. – Dark Templar Oct 17 '19 at 11:38
  • @BonEd I belive you need a Frame control in order to navigate between pages, I don't think windows can be navigated in WPF. – Dark Templar Oct 17 '19 at 11:41
  • @DarkTemplar adun toridas my friend. While you are technically right you are still wrong. Yes if your intend is to place a control on a canvas with a margin it doesnt matter if you enter saied margin or use drag and drop. But propper Xaml does not use margins to position controls in the first place. It uses layout-controls like a grid/stackpanel/dockpanel etc – Denis Schaf Oct 17 '19 at 12:54
  • I do not think that my question is a duplicate of the other one about NullReferenceException. My main problem is that how to navigate between pages and the mainpage and why I got the exception in the above code. Not what is NullReferenceException itself. – BonEd Oct 17 '19 at 13:20

0 Answers0