I put this WPF application http://tanguay.info/web/index.php?pg=codeExamples&id=164 together
http://tanguay.info/web/index.php?pg=codeExamples&id=164
which reads an XML file of customers, allows the user to edit them and saves it back, and it all works well.
However, when the user clicks Save on the "manage customers" page, I want the application to "go back" to the "show customers" page.
The "pages" are user controls being dynamically loaded in the shell like this:
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 is loaded and handles the click, I then try to go back to the CustomersView page, which works, but it opens up a new window so every time the user edits a customer, a new window pops up afterward.
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 one, instead of popping up another instance of the application as it does now?