Reputation: 1268
There is one MainWindow
and one user-control
in my WPF application. I want to call function of my MainWindow
in user-control
, without creating new instance of MainWindow
. For this i made main-window parent of user-control. I wrote this code below for calling function of Parent.
Child User-Control
public partial class AppLogo : UserControl
{
public MainWindow myparent { get; set; }
private void activate_Click_1(object sender, RoutedEventArgs e)
{
myparent.function();
}
. . .
}
Parent Window:
public MainWindow()
{
InitializeComponent();
AppLogo childWindow = new AppLogo();
. . .
Questions:
Window
a parent of user-control
?Yes
then why it is generating error that Object Reference is Null
.No it is not possible
then how can i achieve this goal. As it is necessary to create user-control in my application as it is requirement. Upvotes: 0
Views: 434
Reputation: 19296
If you want to have reference in UserControl
to MainWindow
use following code:
MainWindow mw = Application.Current.MainWindow as MainWindow;
http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow.aspx
private void activate_Click_1(object sender, RoutedEventArgs e)
{
MainWindow mw = Application.Current.MainWindow as MainWindow;
if(mw != null)
{
mw.function();
}
}
Second solution:
In your code you should set myparent
property in MainWindow
constructor:
public MainWindow()
{
InitializeComponent();
AppLogo childWindow = new AppLogo();
childWindow.myparent = this;
...
}
In activate_Click_1
event handler the good habit is check if myparent
is not null:
private void activate_Click_1(object sender, RoutedEventArgs e)
{
if(myparent != null)
myparent.function();
else
...
}
Upvotes: 1
Reputation: 9790
If you locate your UserControl
directly inside your Window
, then its Parent
property will reference your window.
The exception is called when you try to access the field which contains null
value. It contains null
because noone placed there anything else. You might want to set it:
AppLogo childWindow = new AppLogo();
childWindow.myparent = <something>;
You just need to recursively search your UserControl's parents until you get an instance of a Window
, which will be your goal.
public static Window GetWindow(FrameworkElement element)
{
return (element.Parent as Window) ?? GetWindow(element.Parent);
}
Upvotes: 0
Reputation: 3896
You need to pass a reference to the instance of MainWindow as an argument to AppLogo's constructor, then set it to AppLogo's variable for MainWindow.
public AppLogo(MainWindow mainWindow)
{
this.myparent = mainWindow;
}
Upvotes: 0
Reputation: 116
You may introduce a child parent dependecy as suggested, however since you didn't instantiate MainWindow you should expect a null reference exception when calling myparent.function();
First, you need to instantiate MainWindow, and then set the child parent relationship by calling AppLogo .set_myparent, only then your call won't fail.
Upvotes: 0
Reputation: 16423
I'm assuming that the null reference is for the myparent
property on AppLogo
?
After this line AppLogo childWindow = new AppLogo();
add one saying childWindow.myparent = this;
Upvotes: 1