0

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.

3
  • Your question is far too broad as written. There are many ways to implement dynamic content in a program, WPF or otherwise. And in WPF, the difficultly is strongly correlated to how well you have followed the normal MVVM paradigm that WPF is designed to work with (the better you have done so, the easier it will be). IMHO, the easiest is to create views for each state you want to present, as DataTemplate resources, with a different view model class for each state. Then just include a ContentControl where the Content property is bound to the view model for the current state. Commented Feb 22, 2017 at 4:36
  • 1
    i agree with @PeterDuniho - this is too broad. if you can narrow the problem down to one small next step, then you will likely get help with that specific step. Commented Feb 22, 2017 at 4:38
  • My main problem is with creating control, or panel with all controls in other file, and then show it in mainwindow. For example, create class file, which will create all controls, and then show it on the screen. Commented Feb 22, 2017 at 22:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i think that it is what i want :) Thanks a lot!
0

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());
}

1 Comment

that could be helpful :) i will try it but I also would like to know how to create every user control. Where i should keep code-behind with all buttons, textboxes and others? If I understand, I need to create a dinamically space and function which change canvas. But i want to know how i should create this control from other files ( class file, page file etc.) Thanks for help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.