I have a MainWindow, wich Contain a Frame.
My Frame switches between different xaml's using buttons on my MainWindow.
the problem I have now, is that I need to do this also from buttons within the xaml loaded within my Frame.
i tried te following:
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow mw = new MainWindow();
Page myPage = new Page();
mw.editFramePage(myPage);
}
here is my edit editFramePage Method:
public void editFramePage(Page page)
{
myFrame.NavigationService.Navigate(page, UriKind.Relative);
}
This works, however it pops up a new MainWindow, I want this withing my current MainWindow.
Could use some help!
The problem is that you are creating a new MainWindow. You need to keep a reference to the original MainWindow and call editFramePage on that.
The default implementation skeleton of a WPF application saves the main window in App.MainWindow. To get to it, you need to use Application.Current.MainWindow.
So your function should look like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow mw = (MainWindow) Application.Current.MainWindow;
Page myPage = new Page();
mw.editFramePage(myPage);
}
Note that the cast to MainWindow is required because Application.MainWindow returns the type Window instead of the more-specific type that you need.
Related
I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.
i have tested with this
UC lauchUC = new UC(person);
lauchUC.Show();
but that don't show nothing, and also tested with this:
UC lauchUC = new UC(person);
this.Controls.Add(lauchUC);
but it appears in the form
can someone help me or telling me if it's possible show it separated from the form?
You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.
The (container) Form:
public partial class Form1 : Form
{
public Form1(UserControl control)
{
InitializeComponent();
this.Controls.Add(control);
}
}
How to open it.
public void ButtonClick(object sender, EventArgs e)
{
var myControl = new MyUserControl();
var form = new Form1(myControl);
form.Show();
}
You can Place it in a Window and call Window.ShowDialog.
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UC(person)
};
window.ShowDialog();
}
I have been trying to figure out how to switch from my MainWindow.xaml to a WPF Page in my View folder called StudentView.xaml when a button on the MainWindow is clicked.
This is what I have so far...
private void ButtonStartQuiz_Click(object sender, RoutedEventArgs e) {
var window = new View.StudentView();
}
All I want is for the MainWindow to switch to the StudentView page when the user clicks the button. I've tried opening the StudentView as a new Window, but I don't want a new window every time the user clicks a button. I've tried googling and looking at other posts, but I don't understand how I'm supposed to implement them. Please help!
After the button click then, you can create an instance of that page and set the instance as content of your mainwindow.
private NavigatingPageName Instance;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Instance == null)
{
Instance = new NavigatingPageName();
}
this.Content = Instance;
}
If you want to navigate from MainPage.xaml to another .xaml use:
NAMEOFYOURCURRENTFRAME.Navigate(typeof(PAGENAME));
If you want to navigate from a page which isnĀ“t MainPage.xaml to another .xaml use:
var rootFrame = Window.Current.Content as Frame;
var mainPage = rootFrame.Content as MainPage;
rootFrame.Navigate(typeof(PAGETONAVIGATETO));
If you want to keep your MainWindow.xaml content's state alive, then use Frame + Pages.
Otherwise, use ContentControl and use ContentTemplateSelector .
I am pretty new in WPF C#. I am held up with the following Issue:
Lets say I have a MainWindow.xaml, and have the main program logic in it.
I have a second window, called Second.xaml
I am calling Second.xaml in MainWindow.xaml.cs,
currently I am doing:
MainWindow.xaml.cs:
var wind = new Second();
wind.Show();
This successfully opens the second window, where I have a few buttons.
My motive is to trigger events in MainWindow using Second.xaml.cs
(i.e.)
in Second.xaml.cs:
....
..
MainWindow mainwindowID = new MainWindow();
....
..
.
private void nextButton_Click(object sender, RoutedEventArgs e)
{
mainwindowID.textBox.Content = "Displaying In Mainwindow";
}
When I click the Next button in Second.xaml, I want the text Box inside Mainwindow to be updated.
Though the program in running, nothing changes inside MainWindow.
Is it possible to control it like that?
I have the MainWindow displayed using a projector, and the second window on the monitor. So I trigger events inside the second window and want them to be displayed in the MainWindow.
Is there any other solution for this kind?
Update:
If the textbox is inside SecondPage.xaml and displayed inside MainWindow.xaml using a Frame, how do I call it from Second.xaml?
In the first window (MainWindow) you can invoke the second window in this way:
var wind = new Second();
wind.FirstWindow = this;
wind.Show();
while the second window can look like this:
public MainWindow FirstWindow { get; set; }
private void nextButton_Click(object sender, RoutedEventArgs e)
{
FirstWindow.textBox.Content = "Displaying In Mainwindow";
}
I would suggest using Delegates. Have a look at the link here;
Delegates
This way you can create a method in the first Window like so;
private void WaitForResponse(object sender, RoutedEventArgs e)
{
var second = new SecondWindow();
second.ReturnTextBoxText += LoadTextBoxText;
SecondWindow.ShowDialog();
}
Then in your second Window;
internal Action<string, int> ReturnTextBoxText;
private void nextButton_Click(object sender, RoutedEventArgs e)
{
ReturnTextBoxText("Displaying In Mainwindow");
}
Finally load that response in your first Window;
private void LoadSelectedCompany(string text, int passedCompanyID)
{
contactCompanyTextBox.Text = companyName;
}
I have the main window, and another window. in the 2nd window i've created new canvas, and i want to change its properties from the main window, i've failed with this try:
this is the 2nd window's class:
public partial class window2 : Window
{
public Canvas painting = new Canvas();
public window2()
{
}
}
and here i try to change its property from the main window:
window2 paint = new window2();
private void button1_Click(object sender, RoutedEventArgs e)
{
paint.painting.Background = Brushes.Black;
}
when i click the button it does nothing.
Edit:
i think it would be better if i'll use the Application.current.properties and store the canvas object, but i don't know how to use it, i tried this:
Application.Current.Properties["p1"] = painting;
now how can i set properties from the main window with using the "p1" variable i've just created? i tried p1.background but i cant use p1 as variable, so how do i do it?
Your window2 constructor should contain this:
this.AddChild(painting);
Whenever you create a new control (like Canvas) you should set its parent container.
This is my code from window2:
public Canvas painting = new Canvas();
public window2()
{
this.AddChild(painting);
}
And mainwindow:
private void button1_Click(object sender, RoutedEventArgs e)
{
window2 w = new window2();
w.Show();
w.painting.Background = Brushes.Black;
}
What I believe you are saying is that you have an undetermined number of canvases and you want to access them all. I suggest you keeping them in a List of canvases or in a HashTable (you need to use System.Collections namespace). Also don't forget to set the parent container.
A Canvas is a little odd as far as WPF controls go. It likely has a zero size, so you're not seeing the change. Try hard-coding a size to check whether the code is working. In the window2 constructor do this:
painting.Width = 100;
painting.Height = 100;
Can anyone here do me a favor?
I have a MainWindow with a HideButton and RetrieveButton. When I click on either one button, it will goes to another ChildWindow. My ChildWindow have a OKButton.
The question here,
How to set if else statement in C# for pseudocode below?
private void OKButton_Click(object sender, EventArgs e)
{
// pseudocode
if (HideButton in MainWindow is clicked)
{
// Perform the works
}
if(RetrieveButton in MainWindow is clicked)
{
// Perform other works
}
Thanks in advance.
By, Aeris
If you are creating the child window on Retrieve or Hide you can easily pass parameters then either via the constructor or by setting a custom property:
ChildWindow child = new ChildWindow();
child.Retrieving = true;
child.Show();