I have the following classes:
MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainMenu mainMenu = new MainMenu(this);
menuFrame.Navigate(mainMenu);
UserPage userP = new UserPage();
contentFrame.Navigate(userP);
}
public void LoadAPage(Page page)
{
contentFrame.Navigate(page);
}
}
MainMenu:
public partial class MainMenu : Window
{
private Window switchPage;
public MainMenu(MainWindow mainP)
{
Window mainWindow = mainP;
InitializeComponent();
switchPage = mainP;
}
private void btn_navigate_user(object sender, EventArgs e)
{
UserPage userP = new UserPage(ServiceLogic);
//switchPage = Window.GetWindow(PageSwitcher);
//switchPage.LoadAPage(new UserPage());
}
As you can see, I'm trying to use the LoadAPage-method from the MainWindow. The MainMenu and UserPage are childs from the MainWindow. The problem is, no matter what I try, I cannot reach the LoadAPage method. I've tried setting the Owner but that doesn't work. When trying mainMenu.Owner = this;, Visual Studio says Mainmenu does not contain a definition for 'Owner``. When I give the parent class as a parameter to the child class, there are no errors but the methodLoadAPage` is unknown there.
What am I doing wrong? How should I solve this?
EDIT: Changing MainMenu to a Window instead of UserControl makes me able to set the Owner. Still, I can't reach the method.
What a mess :) Try this
private MainWindow switchPage;
public MainMenu(MainWindow mainP)
{
InitializeComponent();
switchPage = mainP;
}
private void btn_navigate_user(object sender, EventArgs e)
{
UserPage userP = new UserPage(ServiceLogic);
switchPage.LoadAPage(userP);
}
However i think you ll be better off using events, instead of passing your window object to its childs.
Related
Hello I hope you can help me, I've been trying like more than 10 days to solve this problem.
I have a Form Application with 1 usercontrol and 1 Class and need use my class instance created in Form1 inside the UserControl1. (With Form1 to Form2 it's works fine)
Class CMensaje:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public class CMensajes
{
private string mensaje;
public CMensajes()
{
}
public string Mensaje { get => mensaje; set => mensaje = value; }
}
}
UserControl1:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class UserControl1 : UserControl
{
CMensajes mensajito;
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = mensa;
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = mensajito.Mensaje;
}
}
}
Form1
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class Form1 : Form
{
CMensajes mensajito = new CMensajes();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
mensajito.Mensaje = textBox1.Text;
UserControl1 usercontrol1 = new UserControl1(mensajito);
}
}
}
The problem is that it's works but intermediately I start to get
The variable mensajito is either undeclared or was never assigned. When I open Form1 Design. When I do the same code with Form1 to Form2 everything perfect!!
Really I need to pass a instance of my class serialport but is the same Here I write only a test code to understand what could I do?
Thanks.
Error Screen when I try to open Form1.designer.cs
For me you should do this inside the User Control constructor:
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = new CMensajes();
mensajito = mensa;
}
Haven't got a machine in front of me to test this but at the risk of sounding silly, try adding a default constructor to your user control.
public UserControl1()
{
InitializeComponent();
}
Or try moving the instantiation of your class inside the Form constructor.
I have a main window and it has a button. When I press the main window button, it will open sub-window. In sub window, I have a text box and when I enter the text and click the add button in sub-window, the text should display on the main window. How do I achieve this? Thanks in advance.
You can create parameterized constructor of MainWindow class and pass value of Textbox from subwindow to main window and in MainWindow you can set content to that label.
Here is the implementation
MainWindow.xmal.cs
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent();
}
public MainWindow (string text) : this()
{
label.Content = text;
}
private void button_Click (object sender, RoutedEventArgs e)
{
Window1 win1 = new Window1();
win1.Show();
this.Close();
}
}
Here is the code for subWindow i.e. Window1.xaml.cs
public partial class Window1 : Window
{
private string text;
public Window1 ()
{
InitializeComponent();
}
private void button_Click (object sender, RoutedEventArgs e)
{
text = textBox.Text;
MainWindow mainWindow = new MainWindow(text);
mainWindow.Show();
this.Close();
}
}
You can simply modify the constructor of the second window :
public partial class Window1 : Window
{
string text;
public Window1 (string _text)
{
InitializeComponent();
this.text = _text;
}
}
I would add an event to your 'dialog' subwindow which the MainWindow can subscribe to as it instantiates it.
DialogWindow: (subwindow):
public class DialogInputEventArgs : EventArgs
{
public string Input { get; set; }
}
public partial class DialogWindow : Window
{
public event EventHandler<DialogInputEventArgs> InputChanged = delegate { };
private void SubmitInputButton_Click(object sender, RoutedEventArgs e)
{
InputChanged(this, new DialogInputEventArgs() { Input = this._inputTextBox.Text });
}
}
MainWindow:
private void ShowDialogButton_Click(object sender, RoutedEventArgs e)
{
DialogWindow dw = new DialogWindow();
dw.InputChanged += OnDialogInputChanged;
dw.Show();
}
private void OnDialogInputChanged(object sender, DialogInputEventArgs e)
{
// update the MainWindow somehow using e.Input (the text submitted in dialog)
}
If you need this mechanism for multiple windows I would go with something more generic, like a messagebus or observerpattern thing.
I'd suggest you to use CaliburnMicro framwework to achieve easier and better communication between your controls (assuming you are using MVVM pattern). You don't have to implement all features of CaliburnMicro, just EventAggregator to manage sending and handling messages between your controls.
Is there another way to access MainWindow's public variables than :
MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
mainWindow.variable....
this work fine, but I'm creating a WPF application and integrating a USB Webcam to my project and using this code above to access MainWindow's variables. This causes some problems like program is still running when I close MainWindow and camera won't stop.
Any suggestions?
This is an over simplified example for what i wrote in comments(you really should look at mvvm the example below is not mvvm).
public class SelectedIndexData
{
public int SelectedIndex { get; set; }
}
public partial class MainWindow : Window
{
private readonly SelectedIndexData _selectedIndexData = new SelectedIndexData();
public MainWindow()
{
InitializeComponent();
}
private void ComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
_selectedIndexData.SelectedIndex = ComboBox.SelectedIndex;
}
private void ShowChildWindow(object sender, RoutedEventArgs e)
{
new ChildWindow(_selectedIndexData).Show();
}
}
public partial class ChildWindow : Window
{
private SelectedIndexData _selectedIndexData;
public ChildWindow(SelectedIndexData selectedIndexData)
{
InitializeComponent();
_selectedIndexData = selectedIndexData; // do whatever you want with your data here
}
}
This approach work also if you are developing a dll (you can't tell the same about Application.Current.MainWindow, which is clearer then your attempt anyways)
DependencyObject ucParent = this.Parent;
while (!(ucParent is MainWindow))
{
ucParent = LogicalTreeHelper.GetParent(ucParent);
}
mainview = ucParent as MainWindow;
You can pack this inside a method, save the variables that you need and you shouldn't have problems
To make sure you application shuts down when MainWindow is closed:
public MainWindow()
{
InitializeComponent();
this.Closing += (sender, args) => Application.Current.Shutdown();
}
I as the title says. I want to be able to change the GUI mainwindow from other classes. When i have all methods in the mainwindow class everything workes fine. But when i use another class in same namespace it doesn't work. My code compiles but when i click on button nothing happens.
class w_Kcal
{
MainWindow mw;
public w_Kcal(MainWindow mw)
{
this.mw = mw;
mw.maintenanceButton.Click += MaintenanceButton_Click;
}
public void MaintenanceButton_Click(object sender, RoutedEventArgs e)
{
mw.maintenanceBox.Visibility = Visibility.Visible;
mw.maintenanceOKBtn.Visibility = Visibility.Visible;
}
}
Mainwindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
It happens because you do not instantiate w_Kcal anywhere. Create this class in main window code and it should work:
public partial class MainWindow : Window
{
private w_Kcal helper;
public MainWindow()
{
InitializeComponent();
helper = new w_Kcal(this);
}
}
Hope this helps
I have a mainwindown and I want to open a window inside that stays on top of it. But it seems like the window I want inside is opened before the mainwindow opens.
To solve this I need to open the window after initializecomponent of the mainwindow?
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent();
OpenProjectsView();
}
private void OpenProjectsView()
{
ProjectsView projectWindow= new ProjectsView();
projectWindow.Owner = this;
projectWindow.ShowDialog();
}
}
Call OpenProjectsView( ) in Form.Activated event.
private void MainWindow_Activated(object sender, System.EventArgs e)
{
OpenProjectsView();
}
try this code
projectWindow.IsMdiContainer = True;
projectWindow.MdiParent = this;
projectWindow.Show();
Try this. It will launch your window after your main window is rendered.
public MainWindow()
{
InitializeComponent();
this.ContentRendered+= Window_ContentRendered;
}
private void Window_ContentRendered(object sender, RoutedEventArgs e)
{
OpenProjectsView();
}
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent();
OpenProjectsView();
}
private void OpenProjectsView()
{
ProjectsView projectWindow= new ProjectsView();
projectWindow.Owner = this;
projectWindow.TopMost = true;
projectWindow.Show();
}
}
This will make the second window sit on top of the first, but because you create and show it during your parent window constructor, it will always appear (get instantiated) before your parent window.
This method allows that to happen, whilst you can still interact with both windows (Show() rather than ShowDialog()).
As others have mentioned, you can instantiate your window in an event, rather than the constructor, but that depends on exactly what you need ... your question is a little ambiguous! :O)