Click here to Skip to main content
14,766,249 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to do a WPF Application,which need to contain menu bar. When user click every menu item , window should be loaded with different contents. please any one suggest any ideas to accomplish this.

What I have tried:

<Window  x:Class="win3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        Title="MainWindow" Height="700" Width="600" >
    <Grid>

        <DockPanel>
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="Student" Click="StudentMenuItem_Click"/>
                <MenuItem Header="Academic" Click="AcademicMenuItem_Click"/>
            </Menu>
            <TextBox AcceptsReturn="True" />
        </DockPanel>
        <Grid x:Name="grdStudent"  Height="500" Width="550" Margin="22,56,22,115">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="3*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="6*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Label Content="Student Id" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="57,51,0,0"/>
            <Label Content="Student Name: " HorizontalAlignment="Left" Margin="57,110,0,0" VerticalAlignment="Top" Width="84"/>
            <TextBox Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="46,51,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="175"/>
            <TextBox Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="46,110,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="175"/>
        </Grid>

        <Grid x:Name="grdAcademic" Height="500" Width="550" Margin="22,56,22,115" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="3*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="5*"/>
            </Grid.RowDefinitions>
            <ComboBox x:Name="cmbFloor" HorizontalAlignment="Left" VerticalAlignment="Top" Width="80" SelectionChanged="cmbFloor_SelectionChanged" Margin="130,29,0,0"/>
            <ComboBox x:Name="cmbDoor" HorizontalAlignment="Left" Margin="130,10,0,0" VerticalAlignment="Top" Width="80" SelectionChanged="cmbDoor_SelectionChanged" Grid.Row="1"/>
        </Grid>
    </Grid>
</Window >



.cs

private void StudentMenuItem_Click(object sender, RoutedEventArgs e)
       {
           this.grdStudent.Visibility = Visibility.Visible;
           this.grdAcademic.Visibility = Visibility.Collapsed;
       }

     private void AcademicMenuItem_Click(object sender, RoutedEventArgs e)
       {

           this.grdAcademic.Visibility = Visibility.Collapsed;
           this.grdStudent.Visibility = Visibility.Collapsed;
       }


Above code works as per expected.but .XMAL file looks clumsy.(Later we need to add more controllers)
Posted
Comments
Alexander Dymshyts 30-Oct-16 10:40am
   
You can create two userControls - one for user and one for academic and load them on click event into your main view
johannesnestler 2-Nov-16 10:04am
   
try MVVM pattern...
Jitesh Hirani 7-Nov-16 6:20am
   
Please see following Link
http://stackoverflow.com/questions/14098810/wpf-add-a-command-to-auto-generated-by-binding-menu-items

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900