Reputation: 3924
Reuirement :
Need to show all child pages in a master layout. Without opening as new window i mean it should not be visible as separate window from main window
Solution i figured :
I used content presenter in main page.
Create all other pages as User controls.
On click of menu ViewWindow.Content = new SalesEntry();
By using that i am showing that.
Problem :
To close that user control i used a button click (button present inside the user control)
to preform this.Visibility = Visibility.Hidden;
But every time when user request this page the page is initialized and shown.
So, whats the best approach for this to overcome or any other way to solve this. (I was told not to use any framework as a project requirement)
I am very new WPF..
Please help me in this..
Upvotes: 1
Views: 767
Reputation: 3149
What you are doing is fine, I don't really understand the problem here but I will tell you how I would do it.
You will have a parent view, a Window. You will have many childs, UserControl's.
Inside your window, you should have a way of selecting which child to show. This can be done using buttons or a menu.
When you select a child, you instantiate it as an object and subscribe to its exit event. When this event is fired by the child, you remove that child from your childs in the parent window.
// This one defines the signature of your exit event handler
public delegate void OnExitHandler(UserControl sender);
// This is your child, UserControl
public partial class MyChild : UserControl
{
public event OnExitHandler OnExit;
public MyChild()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.OnExit(this);
}
}
// This is your parent, Window
public partial class MainWindow : Window
{
private MyChild _control; // You can have a List<UserControl> for multiple
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_control = new MyChild();
_control.OnExit += _control_OnExit; // Subscribe to event so you can remove the child when it exits
_content.Content = _control; // _content is a ContentControl defined in Window.xaml
}
private void _control_OnExit(UserControl sender)
{
if(sender == _control)
{
// Or if you have a collection remove the sender like
// _controls.Remove(sender);
_control = null;
_content.Content = null;
}
}
}
If your problem is something else, please comment.
Upvotes: 1