Menu item action handler : Menu « Windows Presentation Foundation « C# / C Sharp






Menu item action handler

Menu item action handler
 
<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="FlowDocReader Load/Save Sample" Width="640" Height="480">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="22"/>
      <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Menu Grid.Row="0">
      <MenuItem>
        <MenuItem.Header>File</MenuItem.Header>
        <MenuItem Click="LoadFile">
          <MenuItem.Header>Load</MenuItem.Header>
        </MenuItem>
        <MenuItem Click="SaveFile">
          <MenuItem.Header>Save As...</MenuItem.Header>
        </MenuItem>
        <MenuItem Click="Clear">
          <MenuItem.Header>Clear Content</MenuItem.Header>
        </MenuItem>
        <MenuItem Click="Exit">
          <MenuItem.Header>Exit</MenuItem.Header>
        </MenuItem>
      </MenuItem>
    </Menu>
    
    <FlowDocumentReader Name="FlowDocRdr" Grid.Row="1"/>
    
  </Grid>
</Window>

//File:Window.xaml.cs
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.Windows.Forms;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        void LoadFile(Object sender, RoutedEventArgs args)
        {
            Console.WriteLine("load");
        }

        void SaveFile(Object sender, RoutedEventArgs args)
        {
            Console.WriteLine("save");
        }

        void Clear(Object sender, RoutedEventArgs args) { FlowDocRdr.Document = null; }
        void Exit(Object sender, RoutedEventArgs args) { this.Close(); }
    }
}

   
  








Related examples in the same category

1.File menuFile menu
2.Menu and MenuItemMenu and MenuItem
3.Menu With Sub headingMenu With Sub heading
4.A main menuA main menu
5.MenuItems with CommandsMenuItems with Commands
6.Menu item with shortcut and access keyMenu item with shortcut and access key
7.Window with Menu, ToolBar, StatusBarWindow with Menu, ToolBar, StatusBar
8.MenuItem Commands And EventsMenuItem Commands And Events
9.Add MenuItem to MenuAdd MenuItem to Menu
10.MenuItem opened eventMenuItem opened event
11.Use MenuItem.Tag to store user objectUse MenuItem.Tag to store user object
12.Format TextBox with MenuItem: normal, bold, italicFormat TextBox with MenuItem: normal, bold, italic
13.MenuBar and ToolBarMenuBar and ToolBar
14.Add Menu to NavigationWindow ContentAdd Menu to NavigationWindow Content