MenuItem Commands And Events : Menu « Windows Presentation Foundation « VB.Net Tutorial






<Window x:Class="MenuItemCommands.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MenuItem Commands and Events Sample" Height="300" Width="300">
  <StackPanel>
    <Menu>
      <MenuItem Header="_Edit">
        <MenuItem Command="ApplicationCommands.Copy"/>
        <MenuItem Command="ApplicationCommands.Cut"/>
        <MenuItem Command="ApplicationCommands.Paste"/>
      </MenuItem>
      <MenuItem Header="_Font">
        <MenuItem Header="_Bold" IsCheckable="True" Checked="Bold_Checked" Unchecked="Bold_Unchecked"/>
        <MenuItem Header="_Italic" IsCheckable="True" Checked="Italic_Checked" Unchecked="Italic_Unchecked"/>
        <Separator/>
        <MenuItem Header="I_ncrease Font Size" Click="IncreaseFont_Click"/>
        <MenuItem Header="_Decrease Font Size" Click="DecreaseFont_Click"/>
      </MenuItem>
    </Menu>
    <TextBox Name="textBox1" TextWrapping="Wrap" Margin="2">
      this is a test
    </TextBox>
  </StackPanel>
</Window>
//File:Window.xaml.vb

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes


Namespace MenuItemCommands
  Public Partial Class Window1
    Inherits System.Windows.Window
    Public Sub New()
      InitializeComponent()
    End Sub
    Private Sub Bold_Checked(sender As Object, e As RoutedEventArgs)
      textBox1.FontWeight = FontWeights.Bold
    End Sub

    Private Sub Bold_Unchecked(sender As Object, e As RoutedEventArgs)
      textBox1.FontWeight = FontWeights.Normal
    End Sub
    Private Sub Italic_Checked(sender As Object, e As RoutedEventArgs)
      textBox1.FontStyle = FontStyles.Italic
    End Sub

    Private Sub Italic_Unchecked(sender As Object, e As RoutedEventArgs)
      textBox1.FontStyle = FontStyles.Normal
    End Sub

    Private Sub IncreaseFont_Click(sender As Object, e As RoutedEventArgs)
      textBox1.FontSize += 2
    End Sub

    Private Sub DecreaseFont_Click(sender As Object, e As RoutedEventArgs)
      textBox1.FontSize -= 2
    End Sub
  End Class
End Namespace
WPF Menu Item Commands And Events








16.22.Menu
16.22.1.Mixed MenusMixed Menus
16.22.2.Sidebar MenuSidebar Menu
16.22.3.Put a Menu and toolbar on the top of a DockPanelPut a Menu and toolbar on the top of a DockPanel
16.22.4.Nesting content inside MenuItem.HeaderNesting content inside MenuItem.Header
16.22.5.Multiple command targetsMultiple command targets
16.22.6.Commands with a menu and text boxCommands with a menu and text box
16.22.7.Set Access key for Menu with underscoreSet Access key for Menu with underscore
16.22.8.Menu and MenuItemMenu and MenuItem
16.22.9.MenuItem Commands And EventsMenuItem Commands And Events
16.22.10.Display a Context Menu with OpacityDisplay a Context Menu with Opacity
16.22.11.MenuBar and ToolBarMenuBar and ToolBar
16.22.12.Add Menu to NavigationWindow ContentAdd Menu to NavigationWindow Content
16.22.13.Menu item action handlerMenu item action handler