Reputation: 891
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:
Thanks in advance.
Upvotes: 1
Views: 763
Reputation: 169160
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;
}
}
Upvotes: 1