MenuItem Commands And Events

image_pdfimage_print


   
 

<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.cs

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


namespace MenuItemCommands
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Bold_Checked(object sender, RoutedEventArgs e)
        {
            textBox1.FontWeight = FontWeights.Bold;
        }

        private void Bold_Unchecked(object sender, RoutedEventArgs e)
        {
            textBox1.FontWeight = FontWeights.Normal;
        }
        private void Italic_Checked(object sender, RoutedEventArgs e)
        {
            textBox1.FontStyle = FontStyles.Italic;
        }

        private void Italic_Unchecked(object sender, RoutedEventArgs e)
        {
            textBox1.FontStyle = FontStyles.Normal;
        }

        private void IncreaseFont_Click(object sender, RoutedEventArgs e)
        {
            textBox1.FontSize += 2;
        }

        private void DecreaseFont_Click(object sender, RoutedEventArgs e)
        {
            textBox1.FontSize -= 2;
        }
    }
}