Search code examples
c#wpfwindowpanels

WPF application in one window


I am new in WPF and want to create WPF application like cookbook. I already done this and app work correctly. But I make it in this way:

First screen show buttons, which open new windows to do something. As a result i have 14 different windows. It is ok, but now i want to make it in other way.

I am trying to make one window, which will be showed at start, and change content. I divided window on two grids. First is static and is placed on bottom. It contains buttons, which represents functionality of the program. Second one will be dynamic. There i want to show content of every window. So i want to change content of this panel instead of creating new windows.

I tried to make *.cs files which will create controls in code-behind, functions and data. But my idea is not succesful and i do not know how to do this.

At all, I want to create app, which will work like this: - if you click button "Add receip" then app will show controls to add name, ingredients and save it at the end. - if you clik "Show receip" previous content will be replaced by list of ingredients

and etc.

I hope you will understand me.


Solution

  • You can create a Frame instead of second grid. Frame allows you to show pages, and not in seperate windows, in Frame itself. You can navigate the frame into the page like

    mainFrame.Source = new Uri("Page1.xaml",UriKind.Relative);
    

    This changes the frame to your page. You can change the source again, if you wanna change the page again.

    Note: You can add tags to your buttons like "showReceip" and you can make just one buttonclick event for your buttons. Code will look like this.

    mainFrame.Source = new Uri((sender as Button).Tag.ToString() + ".xaml",UriKind.Relative);
    

    That takes the tag of your clicked button, add the string ".xaml" on it and take it on the source part. So, if your tag is "Page1", Source will look like "Page1.xaml" as my solution.