Friday, 2 March 2012

showing multiple forms using one button click in WPF

 Displaying different forms using one button click in WPF
* Code For Design file

<Window x:Class="LayoutPanels.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Menu" Height="400" Width="252"
    >
    <Grid>
      <StackPanel Margin="5" Button.Click="ButtonClick">
        <Button>SimpleStack</Button>
        <Button>SimpleWrap</Button>
        <Button>SimpleDock</Button>
        <Button>BasicDialogBox</Button>
        <Button>SimpleGrid</Button>
        <Button>LayoutRoundingTest</Button>
        <Button>SplitWindow</Button>
        <Button>DoubleSplitWindow</Button>
        <Button>SharedSizeGroup</Button>
        <Button>SimpleCanvas</Button>
        <Button>SimpleInkCanvas</Button>
        <Button>TheUniformGrid</Button>
        <Button>TextBoxColumn</Button>
        <Button>LocalizableText</Button>
        <Button>ModularContent</Button>
      </StackPanel>
       
    </Grid>
</Window>

* Code for .cs file

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 LayoutPanels
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();           
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            // Get the current button.
            Button cmd = (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 + "." + cmd.Content);

            // Show the window.
            win.ShowDialog();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}





No comments:

Post a Comment