Access a parent window control(e.g a Label) inside Child page - c#

I am developing a WPF application where i have a MainWindow that has a Frame control and a Label Control inside it, i have set the source of the Frame control to one of the pages i have created, what i want is to be able to change the content of Label control (inside Main window) from my Page.xaml which is displayed inside the Frame control.
Here is an Image to further illustrate what i really want . excuse my drawing!!

Page page = new Page(this);
Framme.Content = page;

Related

Add WebView2 control to tabitem and navigate to website

I'm fiddling around with Microsofts WebView2 control. I created a simple wpf application, nothing fancy: Just MainWindow with a tabcontrol. Every tab has it's own user data directory.
Then I added a method to instantiate a tabitem and a webview and then add that control to the tabcontrol in MainWindow.
What I have so far - which is working (but...):
private async void AddTab(string serviceName, string serviceUrl)
{
TabItem tabItem = new TabItem();
tabItem.Name = serviceName;
tabItem.Header = serviceName;
myTabControl.Items.Add(tabItem);
// Create a unique user data directory for this tab
// and keep it "portable", a.k.a. in the programm directory
string userDataDirectory = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Sessions\\" + serviceName;
// Create a new WebView2 control and pass custom user data directory
WebView2 webView = new WebView2() { CreationProperties = new CoreWebView2CreationProperties() { UserDataFolder = userDataDirectory } };
// set the source address
webView.Source = new Uri(serviceUrl);
// add webview to tab content
tabItem.Content = webView;
// Initialize the WebView2 control
await webView.EnsureCoreWebView2Async();
}
This works as intended. But as it is now, the webview content is only loaded when I click on the tab. I'm not sure how to make the tabs load in the background, so that when I click on the tab, the website is already loaded.
I found one example on stackoverflow, but it is too complex for what I want to accomplish.
My experiment with a Task brought me into the crazy world of not being able to access my thread again. Such code is way over my head (for now). I'm still learning.
I would try defining the tabitems in xaml.
The tabcontrol is an itemscontrol. I would usually bind the itemssource to a collection of viewmodels and template those out into ui. You only get whichever one of those items is selected instantiated if you bind the items like that. Defining tabitems in xaml seems to mean they're all instantiated. It's noticeably slower.
You can add other tabs afterwards in code if you wanted. But I'd check what happens with some defined in xaml first.
Another thing you could try is hidden webview2 controls in the main view. Navigate to these fixed url immediately the window renders.
Move them out the window and into your tabitem on selecting that tab, set isvisible true. Then you're just incurring rendering.
Btw
The webview2 is a wrapper around a chromium edge browser. It displays some odd behaviour like it can overflow parent containers.

Change Frame Source Wpf

How can I possibly change the source of a frame from other page when I click a button in an active page.
What I'm doing is In page1.xaml, I have a frame with a source displaying page2.xaml.Once I click a button in page2.xaml, I want to update the source of frame in page1.xaml to page3.xaml and the frame should display page3.xaml instead of page2.xaml.
As of now, I tried using
page1 pg1 = new page1();
pg1.frame.source = new Uri("page3.xaml",UriKind.Relative);
But it didn't display page3.xaml in the frame in page1.xaml.
I also tried
page1 pg1 = new page1();
pg1.frame.Navigate(new Uri("page3.xaml",UriKind.Relative));
but didn't work as well, page2.xaml remains the display of the frame.
First of all, it is unusual to have frame inside page. Usually you have frame inside MainWindow or Usercontrol, because Page is meant to be hosted inside frame.
Second, you can't just create new Page1 and use it's frame. This newly created Page1 exist only in memory and it is another instance that the one displayed.
Because it would be quite difficult to find the Frame from pages, there is NavigationService property in Page class.
NavigationService.Navigate(new Uri("page3.xaml",UriKind.Relative));

Change page inside a frame from another frame

In my MainPage I have 3 frames,and trying to change the page of my FrameMain(frame) to another page,the problem is that I am doing this in another page inside a second frame if I use
NavigationService.Navigate(new Uri("/View/CreateAcc.xaml", UriKind.Relative));
It loads my current frame with the page,but I want the FrameMain to load this page, so I tried:
MainPage main = new MainPage();
main.FrameMain.Navigate(new Uri("/View/CreateAcc.xaml", UriKind.Relative));
wich didn't do anything...
sorry about the confuse text if someone can help i only saw examples of changing pages inside the MainPage.
Change the way of doing to get what i wanted,instead of using frame implemented two WrapPanel at exactly the same position and used the property Visibility to put one Visible and the other Collapsed and vice verse to change Panel showing.

Can I add already created pages to tab control?

I have a tab control with tabs on it in a windows application. I have already created some pages. Can I make these pages display on tabs of the tab control?
Thanks in advance.
Make Usercontrols of your pages and add them as TabPages
edit:
To add forms to a TabControl, you will first want to set a Panel inside of a page in the TabControl. Then, do the following in the constructor of the form with the TabControl:
FormToLoad formToLoad = new FormToLoad();
formToLoad.TopLevel = false;
panelInTabPage.Controls.Add(formToLoad);
formToLoad.Show();
You may also want to individually style your forms so that they don't show window chrome.

Showing MultiPle UserControl in a Window as ChildWindows

I have developed a WPF ERP in which i have a single main window and rest windows i have created as UserControls...
Currently I am displaying the user controls in a ContentFrame in the MainWindow but my problem is that i can only display a single user control in a WPF Content Frame and i cant create a Content Frame dynamically...
How can i overcome this...
I have opened the child window as
Stock s=new Stock(); //UserControll
mainGrid.Children.Add(s);

Categories

Resources