I am trying to access parent window from user control.
userControl1 uc1 = new userControl1();
mainGrid.Children.Add(uc1);
through this code I load userControl1 to main grid.
But when I click on a button inside userControl1 then I want to load another userControl2 into mainGrid which is in main window?
Have you tried
Window yourParentWindow = Window.GetWindow(userControl1);
This gets the root level window:
Window parentWindow = Application.Current.MainWindow
or the immediate parent window
Window parentWindow = Window.GetWindow(this);
The only reason why the suggested
Window yourParentWindow = Window.GetWindow(userControl1);
didnt work for you is because you didn't cast it to the right type:
var win = Window.GetWindow(this) as MyCustomWindowType;
if (win != null) {
win.DoMyCustomWhatEver()
} else {
ReportError("Tough luck, this control works only in descendants of MyCustomWindowType");
}
Unless there has to be way more coupling between your type of windows and your control, I consider your approach bad design .
I'd suggest to pass the grid on which the control will operate as a constructor parameter, make it into a property or search for appropriate (root ?) grid inside any Window dynamically.
Modify the constructor of the UserControl to accept a parameter of MainWindow object. Then pass the MainWindow object to the UserControl when creating in the MainWindow.
MainWindow
public MainWindow(){
InitializeComponent();
userControl1 uc1 = new userControl1(this);
}
UserControl
MainWindow mw;
public userControl1(MainWindow recievedWindow){
mw = recievedWindow;
}
Example Event in the UserControl
private void Button_Click(object sender, RoutedEventArgs e)
{
mw.mainGrid.Children.Add(this);
}
Thanks for help me guys. i got another solution
((this.Parent) as Window).Content = new userControl2();
this is perfectly works
Make a static instance of main window ,you can simply call it in your user control:
See this example:
Window1.cs
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
_Window1 = this;
}
public static Window1 _Window1 = new Window1();
}
UserControl1.CS
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void AddControl()
{
Window1._Window1.MainGrid.Children.Add(usercontrol2)
}
}
Related
I am trying to change the label and text of a label which is inside a UserControl from within a Parent Form method at runtime.
So within the method in parent Form I do the following to change the label properties which is inside a UserControl
public partial class Form : Form
{
public void Form_Method()
{
UserControl uc = new UserControl();
uc.UpdateLabel(true);
}
}
And the custom method inside my UserControl
public partial Class UserControl : UserControl
{
public void UpdateLabel(bool value)
{
if (value)
{
lbl.Text = "This";
lbl.Forecolor = Color.Green;
}
if (value == false)
{
lbl.Text = "That";
lbl.Forcolor = Color.Red;
}
}
}
However when I navigated to the UserControl the label properties have not changed as I was creating a new instance of the usercontrol on the fly which
technically disappears after the method ends.
So I tried creating a public property of the actual UserControl as follows
public partial class Form : Form
{
public UserControl _uc;
public void Form_Method()
{
UserControl uc2 = new UserControl();
uc2.UpdateLabel(true);
_uc = uc2;
}
}
However it has no effect whatsoever? I have come across info of using Events or Delegates am not sure if they are the correct process for what am trying to do?
i'm coding a c# application using WPF
i have a main Window which contain a Grid named " SelectionGrid ". this grid will contain Control User, my problem is that i want to modify ( add/delete) Control User in that grid from a USER CONTROL itself
for example:
SelectionGrid host the User Control " Menu" in this menu there a button, i want from this button to remove the Menu User Control and add another User Control in this SelectionGrid
main window code :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UserControl usc = new Menu();
SelectionGrid.Children.Add(usc);
}}
Menu User Control code :
public partial class Menu : UserControl
{
public Menu()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// want to add Another User Control in SelectionGrid
}
Firstly,put your userControl in some container control like Grid.Then you can easily access and modify the grid from the usercontrol as follows:
var parent = (Grid)this.Parent;
///do what you want to do with parent
A bit of knowledge-sharing : The below code can be used to access the parent of controls like Page,UserControl :
public static T FindParent(DependencyObject child)
{
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
if (parentObject == null)
return null;
T parent = parentObject as T;
if (parent != null)
return parent;
else
return FindParent<T>(parentObject);
}
private void test()
{
ControlTypeHere parent = FindParent<ControlTypeHere>(this);
Hope this helps :)
I have the mainwindow. I call the Usercontrols in the mainwindow. I call Usercontrol1 in the mainwindow.loaded section. I would like to bring Usercontrol2 instead of Usercontrol1 by clicking on a button inside Usercontrol1.
My usercontrol caller class:
public class uc_call
{
public static void uc_add(Grid grd, UserControl uc)
{
if (grd.Children.Count > 0)
{
grd.Children.Clear();
grd.Children.Add(uc);
}
else
{
grd.Children.Add(uc);
}
}
}
My Mainwindow_Loaded(It works):
uc_call.uc_add(Content, new UserControl1());
Button Click function in UserControl1:
MainWindow mw = new MainWindow();
uc_call.uc_add(mw.Content, new Usercontrol2());
Do not create a new MainWindow. Instead, use the existing one:
var topLevelPanel = Application.Current.MainWindow.Content as Panel;
if (topLevelPanel != null)
{
topLevelPanel.Children.Clear();
topLevelPanel.Children.Add(new Usercontrol2());
}
Note that is doesn't hurt to call Children.Clear() even when the collection is empty.
In case you've added another Content property that holds the Grid where you want to replace the child elements:
var mainWindow = (MainWindow)Application.Current.MainWindow;
var grid = mainWindow.Content;
grid.Children.Clear();
grid.Children.Add(new Usercontrol2());
or with your static method:
var mw = (MainWindow)Application.Current.MainWindow;
uc_call.uc_add(mw.Content, new Usercontrol2());
I have a window popping up, and a MainWindow is created if one doesn't exist already:
if (App.Current.MainWindow != null && App.Current.MainWindow.GetType() == typeof(MainWindow))
{
this.Close();
}
else
{
//main Window hasn't been created yet so create it!
MainWindow main = new MainWindow();
App.Current.MainWindow = main;
this.Close();
main.Show();
wndw = main;
}
How do I refer to the MainWindow so I can use it outside the if statement?
I want to be able to do main.txbox.Text = .... etc.
Here's what I've tried:
MainWindow main;
if
{...
}
else
{...
}
main= App.Current.MainWindow;
or MainWindow main = App.Current.MainWindow();
Neither of these approaches seem to work. I'm sure this is really simple, I just can't figure out the syntax. Help?
Edit: Similar to this question I asked earlier, but different because here I'm asking about syntax when referencing the currently opened window. The similar question is referring to how to detect which window is opened. They're similar, but I didn't want to go off topic.
Try this way
var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
In some base class or App.xaml.cs create this:
public static Window ActivatedWindow {get;set;}
Then put in your base class deriving Window or all of your Window's Activate Event:
First Option - personal Window Base Class:
public class MetroToolWindowBase
{
public MetroToolWindowBase()
{
Activated += new EventHandler(MakeActive);
}
private void MakeActive(object sender, EventArgs e)
{
App.ActivatedWindow= this;
}
}
Second Option- In Activated Event of Windows:
private void XWindow_Activated(object sender,EventArgs e)
{
App.ActivatedWindow= this;
}
I want to close a window form that is hosting a WPF user control. Something like this as used while closing a current form in window application. But for WPF application I am not able to get reference to user controls parent
How to get Form which is hosting this control so that I can close my form
this.Close()
Add to your WpfControl property
public Form FormsWindow { get; set; }
In your WinForm add event handler for ElementHost's event ChildChanged:
using System.Windows.Forms.Integration;
public MyForm() {
InitializeComponent();
elementHost.ChildChanged += ElementHost_ChildChanged;
}
void ElementHost_ChildChanged(object sender, ChildChangedEventArgs e) {
var ctr = (elementHost.Child as UserControl1);
if (ctr != null)
ctr.FormsWindow = this;
}
After that you can use the FormsWindow property of your WpfControl to manipulate window. Example:
this.FormsWindow.Close();
An alternative solution could be,
Window parent = Window.GetWindow(this);
parent.Close();
Just want to add to #The_Smallest's otherwise very clear answer.
If you just copy and past the event handler code, you will still need to set your Forms's ChildChanged event to ElementHost_ChildChanged. I missed that step and spent 30 minutes trying to figure out why FormsWindow was null.
In order to call the Form object of the MyControl class already. We have in it a Form field to which we pass an instance object open Form. Having an assigned object we can freely manipulate it (including also call the function form.Close ();
WPF Control (with XAML):
public class MyControl : UserControl
{
public Form form = null;
public MyControl()
{
InitializeComponent();
this.PreviewKeyDown += new KeyEventHandler(HandleEsc);
}
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
form.Close();
}
}
}
Form:
public class MainForm
{
//...
public Form form = null;
public MainForm(MyControl myControl)
{
InitializeComponent();
//...
myControl.form = (Form)this;
}
}