Ron
Ron

Reputation: 4095

WPF pass window as variable

I have 4 windows.
1. SubjectMovies.xaml
2. SpecificMovies.xaml
3. SearchMovies.xaml
4. VideoPlayer.xaml

All the first 3 windows can open the forth one.
I want to know which one openend the fourth when the fourth is opened and store it in variable (to use it later - I want to use it like that: Sender(as Window).Show()), something like:

Window sender;
public VideoPlayer(Window s)
{
    InitializeComponent();
    sender = s;

}

private void GoBack()
{
    this.Hide();
    sender.Show();
}

Upvotes: 0

Views: 150

Answers (2)

Jeroen van Langen
Jeroen van Langen

Reputation: 22038

I recomment the ShowDialog on the child to achieve this, because the child should not be responsible for showing his parent.

example:

public void OpenVideoPlayer()
{
    VideoPlayer vp = new VideoPlayer();
    this.Hide();
    vp.ShowDialog();
    this.Show();
}

This way the child doesn't depend on a parent. Also if you don't want to hide the parent, but instead to minimize it, the parent is in control of that.

With events:

public void OpenVideoPlayer()
{
    VideoPlayer vp = new VideoPlayer();
    vp.Closed += vp_Closed;
    this.Hide();
    vp.Show();
}

void wnd_Closed(object sender, EventArgs e)
{
    this.Show();
}

Upvotes: 0

Omri Btian
Omri Btian

Reputation: 6547

You want to set the Owner property of the VideoPlayer window. from each window you are opening it:

VideoPlayer vp = new VideoPlayer();
vp.Owner = this;

Inside VideoPlayer you can access it by this.Owner.

No need to recieve it as a parameter in the constructor.

Upvotes: 2

Related Questions