简体   繁体   中英

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.

Appreciate the try, I hope you are looking for WPF user controls instead for separate windows. User controls are similar to windows you can create the UI and functionalities in the user control. I would like to recommend you to design the main window like the following:

<Grid>
    <Canvas Name="canFunctionalButtons">
        <!--Define the buttons inside this canvas
        And allocate proper place for this in the UI
        -->
    </Canvas>

    <Canvas Name="canControlContainer">
        <!--This is to display the user control
        Which can be changed dynamically according to the Button's click
        -->
    </Canvas>
</Grid>

Then you have to add click event for those buttons, which will add specific user control to the canControlContainer canvas. An example for adding an user control to this canvas is as follows, Let btnAddSomething be a button and btnAddSomething_Click be its click event then you can do something like:

private void btnAddSomething_Click(object sender, RoutedEventArgs e)
{
    canControlContainer.Children.Clear(); // will remove previous contols from this canvas
    // UC_AddSomething be the user control that you wanted to add here
    canControlContainer.Children.Add(new UC_AddSomething());
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM