How do I load a new custom control without opening a new window in WPF?
I put this WPF app http://tanguay.info/web/index.php?pg=codeExamples&id=164 together
http://tanguay.info/web/index.php?pg=codeExamples&id=164
which reads the clients' XML file, allows the user to edit them and save them back , and it all works well .
However, when the user clicks the Save button on the Manage Clients page , I want the application to "return" to the Show Clients page.
"Pages" are custom elements that are dynamically loaded into the shell as follows:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Reflection;
using TestDynamicForm123.View;
namespace TestDynamicForm123
{
public partial class Shell : Window
{
private Dictionary<string, IBaseView> _userControls = new Dictionary<string, IBaseView>();
public Dictionary<string, IBaseView> GetUserControls()
{
return _userControls;
}
public Shell()
{
InitializeComponent();
List<string> userControlKeys = new List<string>();
userControlKeys.Add("WelcomeView");
userControlKeys.Add("CustomersView");
userControlKeys.Add("ManageCustomersView");
Type type = this.GetType();
Assembly assembly = type.Assembly;
foreach (string userControlKey in userControlKeys)
{
string userControlFullName = String.Format("{0}.View.{1}", type.Namespace, userControlKey);
IBaseView userControl = (IBaseView)assembly.CreateInstance(userControlFullName);
_userControls.Add(userControlKey, userControl);
}
//set the default page
btnWelcome.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}
private void btnGeneral_Click(object sender, RoutedEventArgs e)
{
PanelMainContent.Children.Clear();
Button button = (Button)e.OriginalSource;
PanelMainWrapper.Header = button.Content;
Type type = this.GetType();
Assembly assembly = type.Assembly;
IBaseView userControl = _userControls[button.Tag.ToString()] as IBaseView;
userControl.SetDataContext();
PanelMainContent.Children.Add(userControl as UserControl);
}
}
}
So when the ManageCustomersView loads and handles the click, I then try to go back to the CustomerView page , which works but opens a new window , so every time the user edits the customer, a new window appears .
private void OnSave(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
{
Customer customer = e.Parameter as Customer;
Customer.Save(customer);
//go back to default back
Shell shell = new Shell();
Button btnCustomers = shell.FindName("btnCustomers") as Button;
btnCustomers.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
shell.Show();
}
How can I change the above code inside one UserControl so that its parent unloads the current user control and loads another, instead of popping up another instance of the application like it does now?
source to share
The new window pops up clearly because you are creating a new Shell object every time OnSave is called. You need to get the parent window of the shell .
Shell parentShell = Window.GetWindow(this) as Shell;
source to share