WPF window content as content of another window - c#

i need to show my window as inside Tab of another window and i do it using the following
var childWindowEditor = new MainWindow(); //window to embed
object content = childWindowEditor.Content; //take out the content of the window to embed
childWindowEditor.Content = null;
EditorTab.Content = content; //just a tab in a tabbed group
its working fine, but the problem is what if i wanted to access the functions of the window class?
i know tha i should create it as user control, but is there a solution without having to create the content of that window as a user control?
would casting the content to the window that i got the content from work?

You should use the MVVM pattern. Your ViewModel would be the DataContext of childWindowEditor. So you could simply do something like
EditorTab.DataContext = childWindowEditor.DataContext;
And that's it. I really recommend you to use MVVM. It makes your life much easier.

You may use Pages and frames to display pages.
Add the frame on tab like this:
<TabItem>
<Frame Name="Frame1"></Frame>
</TabItem>
Show page inside frame:
Frame1.Content = new MyPage1();

Related

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

User Controls and Windows in WPF

I am making a wpf application. I am very new to WPF.
I have a main layout screen which has a background content and other screens that have the layout plus some more controls exclusive to those screens.
I am doing it as:
The main layout screen is a Window and the other dedicated screens are user controls.
For every specialized screen, I am doing this:
Application.Current.MainWindow mw;
mw.Content = nameOfCurrentUserControl;
Is this idea fine?
If I understand correctly you want to have a program with different pages. Like a wizard for instalation.
Then what you want to do is to create a Window (as you did) and inside it add a Frame.
Then you create Pages and in each page you create the layout you want.
Then in your main Window, you create instances of your Pages (mySecondPage = new SecondPage()) and then you Navigate from one page to the other loading them into the frame of the main Window using MyFrame.Navigate(mySecondPage)
Here you have much more information about navigation : http://paulstovell.com/blog/wpf-navigation

DevExpress. DockLayoutManager. DocumentPanel adding. How to get a link to the original window?

I use component DevExpress DockLayoutManager
According to the documentation on their website
http://documentation.devexpress.com/#WPF/DevExpressXpfDockingLayoutPanel_Uritopic
Control property "returns the root element of the Window / Page" - ie Grid.
Example is in their demo application: DockingDemo.Wpf DocumentGroups.xaml
And here is my code
DocumentGroup documentContainer = dockManager.GetItem("documentContainer") as DocumentGroup;
DocumentPanel panel = dockManager.DockController.AddDocumentPanel(documentContainer,new Uri("/WpfSample;component/MyWindows/Win1.xaml", UriKind.Relative));
panel.Caption = "SomeName";
MyWindow win = ((panel.Control as Grid).Parent as MyWindow);
win.DoInit(object Obj);
And when I call (panel.Control as Grid). Parent - I get a link to DockLayoutManager.
I do not quite understand. Window goes where? DockLayoutManager becomes Parent in the logical tree for the Grid of the Windows?
Tell me please - how to get Window, cast it to the required class and call its method?
ie how to make this code work
MyWindow win = ((panel.Control as Grid).Parent as MyWindow);
win.DoInit(object Obj);
According to the documentation, when the URI refers to a Window, the AddDocumentPanel method only loads the specified Window content. The Window object itself, its resources and event handlers are not loaded, and they cannot be accessed via the LayoutPanel.Control property.
You can use a UserControl object instead of the Window one. In this case, the UserControl itself will be loaded and you will be able to access the loaded UserControl via the LayoutPanel.Control property.

Changing Content of Window from Button Click in User Control

I am working on a program that has a main window with only a ContentControl element.
In the class for this main window, I can change the content by ContentHolder.Content = new UserControlMain(). In UserControlMain, I have a button to once again change the content of the window to a new user control.
I am running into issues with changing the window content the second time. If I do not instantiate a class of MainWindow, I cannot access the Content property from anywhere except its own class. I also cannot access the ContentControl element I have associated with that class. However, if I do instantiate a MainWindow object, I run into an error message stating that the content has already been set.
Any tips on how I may get around this?
My advice is:
In MainWindow create a Grid with a name
<Grid name = "mainGrid">
</Grid>
Then, in code behind you can do easily this:
mainGrid.Children.Clear();
mainGrid.Children.Add(new UserControlMain());
Regards,

Multi-Page WPF Application

I am new to desktop application development and have a pretty basic question. I have a WPF form named MainWindow, how should I go about having multiple pages on this, such as "User Management", "Manage Content" etc..
I think I have the following options:
Use multiple forms
Tabs
Group Box?
Any clarification would be great!
Well in my most recent application I started by using a TabControl, that's a safe and rather easy way to go.
Recently switched the tabcontrol with a StackPanel with a series of Expanders inside. I styled the expanders to have them display the header vertically and expand horizontally... somewhat similar to the first xbox dashboard. And it looks and works great! =)
Another alternative would be to use a Page instead of a window... Then you would just have to Navigate to each different page.
EDIT:
Here's an example of a multi-page application... might be close to what you need.
The solution I went with that suited what I was looking for was using WPF Pages but thanks for your answers.
There are many ways to do that, such as creating UserControl and show them in the run time.But using TabControl is fast and safe.
Just Use TabControl and place your pages in tab items .Then hide the header of TabControl by setting the value Visibility="Collapsed" to each TabItem.
The result is as below:
As you see the headers are hide and you can switch to each page you want.
Create
usercontrol(wpf): UserManagement
usercontrol2(wpf) : ManageContent
place control "ContentControl" in the main window
Run the code on click of button:
//Displays usercontrol1
contentControl.content = new UserManagement();
//Displays usercontrol2
contentControl.content = new ManageContent();
Hope this helps you.
I'd like to give you an example of something I have in one of my applications.
The app has two windows: the main window and another one (also derived from Window and equipped with the appropriate buttons and event handlers) that is used as a start dialog. The start dialog is called in the constructor of the main window like this:
public partial class MainWindow : Window
{
startdlg m_dlg;
// ...
public MainWindow()
{
m_dlg = new startdlg();
if ((bool)m_dlg.ShowDialog())
{
// ...
}
else
{
Close();
}
// ...

Categories