using System; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Reflection; namespace WpfResourcesApp4 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private string str1; public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { // Get the current button. Button cmdBtn = (Button)e.OriginalSource; // Create an instance of the window named // by the current button. Type type = this.GetType(); Assembly assembly = type.Assembly; Window win = (Window)assembly.CreateInstance( type.Namespace + "." + cmdBtn.Content); /* with ShowDialog(), Show the window one (only) what ever you choose // disables all other windows in the application, //and returns only when the active window is closed. //This type of window is known as a modal window. //win.ShowDialog(); * * // cmdButton content will read the encapsulated text */ str1 = cmdBtn.Content.ToString(); MessageBox.Show(str1); try { text1.Text += win.Title + "\n"; ; win.Show(); } catch (Exception error) { text1.Text = "\n"+ error.Message.ToString(); switch (str1) { case "Window3": text1.Text =str1 + " & OOOPS!"; text1.Background= Brushes.Red; break; case "No-XBAP1": text1.Text = str1 + " & OOOPS!"; text1.Background= Brushes.Azure; Uri uri= new Uri("Page1.xaml",UriKind.RelativeOrAbsolute); frame1.NavigationService.Navigate(uri); break; case "Beige": text1.Background = Brushes.Beige; break; } } } } }