1

I am developing WPF application with one parent window and multiple child user controls. Here is my parent window xaml:

<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="STUDENT INFORMATION">
            <MenuItem Header="ADD" Cursor="Hand" Click="MenuItem_Click"></MenuItem>
            <Separator></Separator>
            <MenuItem Header="EDIT" Name="mnuEdit" Cursor="Hand" Click="mnuEdit_Click"></MenuItem>
        </MenuItem>            
    </Menu>

    <ContentControl x:Name="childWindow" Margin="10"></ContentControl>
</DockPanel>

This is my C# code to open a child form:

        childWindow.Content = new ctrStudentAdd();

It's working fine. But when I click on Add menu item how can I:

  1. Check whether the selected User Control is already open or not previously?
  2. And if it's open just bring it to front instead of opening a new instance?

Thanks in advance.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
FireFalcon
  • 831
  • 11
  • 23

1 Answers1

1

You could use the is operator to determine whether the Content property is currently set to an ctrStudentAdd:

if(childWindow.Content is ctrStudentAdd)
{
    //...
}

I am note sure what you mean by "bring to front" though. Once you have set the Content property of the ContentControl to another UC the reference to the previous one is gone and the "old" UC is eligible for garbage collection unless there is some other object that holds a reference to it.

If you don't want to create a new instance of the UserControl each time it is displayed in the ContentControl you could keep a reference to it in your window class, e.g.:

public partial class MainWindow : Window
{
    private readonly ctrStudentAdd _ctrStudentAdd = new ctrStudentAdd();
    private readonly ctrStudentEdit _ctrStudentEdit = new ctrStudentEdit();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Switch()
    {
        childWindow.Content = _ctrStudentAdd;
        ...
        childWindow.Content = _ctrStudentEdit;
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • In WinForms when you open multiple child forms, you can bring a specific active form to front by calling `Application.OpenForms['MyFormName"].BringToFront()` – FireFalcon Jan 27 '17 at 12:57
  • So you want to bring the parent *window* in front? Then yoy should call its Activate() method: http://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf – mm8 Jan 27 '17 at 12:58
  • As you've noticed, I have two menu items (UserControls) Add and Edit. When I open Add and then Edit and again Add I want to bring the previously opened Add usercontrol to front. There is no `Activate()` methon for usercontrols. – FireFalcon Jan 27 '17 at 13:02
  • Exactly. And there is no way to bring a UserControl to front. Once you have set the Content property of the ContentControl to another UC the reference to the previous one is gone and eligible for garbage collection unless there is some other object that holds a reference to it. – mm8 Jan 27 '17 at 13:05
  • What do you suggest to use instead of UserCotrol as a Child form for `MainWindow` so I can achieve my goal? So when I open a new child, previously opened one is just hidden and not removed. – FireFalcon Jan 27 '17 at 13:08
  • Your goal is to *not* create a new instance of the UC each time you display it in the ContentControl? – mm8 Jan 27 '17 at 13:09
  • Exactly, that's what I want. – FireFalcon Jan 27 '17 at 13:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134185/discussion-between-it-captain-and-mm8). – FireFalcon Jan 27 '17 at 13:11
  • I see my friend, but what do you suggest to use instead of UC to accomplish my task? Any alternatives? – FireFalcon Jan 27 '17 at 13:15
  • Not really. The only thing you can bring to front is a window. – mm8 Jan 27 '17 at 13:16
  • Then what others use in multiwindow WPF apps? Do they have parent window? I'm just starting up with WPF. – FireFalcon Jan 27 '17 at 13:18
  • A multi window app uses multiple windows...you could create a new window, set its Content property and call its Show/Activate method instead of setting the Content property of a ContentControl. If that's what you want. – mm8 Jan 27 '17 at 13:20
  • My application needs to have a menu bar. Is there any way to open a window under the menu? – FireFalcon Jan 27 '17 at 13:25
  • This is a completely different question. Ask a new question if you have a new issue. Also, I have already told you how to open a window. – mm8 Jan 27 '17 at 14:49