Mahesh Chand
How to Create a Menu Bar in WPF
By Mahesh Chand in WPF on Aug 07 2019
  • Mahesh Chand
    Aug, 2019 7

    You can use the Menu and the MenuItem classes in WPF. Here is the code that creates a menu bar in WPF.

    <Grid x:Name="LayoutRoot"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Menu Height="38" Name="menu1" Margin="10,10,344,0" VerticalAlignment="Top" BorderThickness="2" Foreground="White" FontSize="16" FontWeight="Bold"> <Menu.Background> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#FF3A60AD" Offset="0.528" /> <GradientStop Color="#FF6A85D8" Offset="0.01" /> <GradientStop Color="#FF3464C4" Offset="1" /> <GradientStop Color="#FF202E7E" Offset="1" /> </LinearGradientBrush> </Menu.Background> <Menu.BitmapEffect> <DropShadowBitmapEffect /> </Menu.BitmapEffect> <MenuItem Header="Tools" IsCheckable="true"> <MenuItem.Icon> <Image Source="Tools.png" Width="20" Height="20" /> </MenuItem.Icon> </MenuItem> <MenuItem Header="Settings" IsCheckable="true" Foreground="Orange" FontSize="16"> <MenuItem.Icon> <Image Source="Settings.png" Width="20" Height="20" /> </MenuItem.Icon> </MenuItem> <MenuItem Header="Security" IsCheckable="true" Foreground="White" FontSize="16"> <MenuItem.Icon> <Image Source="Security.png" Width="20" Height="20" /> </MenuItem.Icon> </MenuItem> <MenuItem Header="Database" IsCheckable="true" Foreground="LightGreen" FontSize="16"> <MenuItem.Icon> <Image Source="Database.png" Width="20" Height="20" /> </MenuItem.Icon> <MenuItem.Background> <ImageBrush ImageSource="Database.png" Stretch="Fill" /> </MenuItem.Background> </MenuItem> </Menu> </Grid>

    You can also create a menu at runtine in C#.

    `Menu mainMenu = newMenu();
    mainMenu.Background = Brushes.LightGreen;
    mainMenu.Height = 300;
    mainMenu.Width = 200;
    MenuItem item1 = newMenuItem();
    item1.Width = 50;
    item1.Header = “First”;
    mainMenu.Items.Add(item1);
    MenuItem item2 = newMenuItem();
    item2.Width = 50;
    item2.Header = “Two”;
    item1.Items.Add(item2);
    MenuItem item3 = newMenuItem();
    item3.Width = 50;
    item3.Header = “Third”;
    item1.Items.Add(item3);

    `

    Here is a complete tutorial: Menus in WPF

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS