MenuBar and ToolBar


   
 

<Window x:Class="Commands.TwoDocument"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TwoDocument" Height="300" Width="300">

  <Window.Resources>
    <CommandBinding x:Key="binding" Command="ApplicationCommands.Save" Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0">
      <MenuItem Header="File">
        <MenuItem Command="New"></MenuItem>
        <MenuItem Command="Open"></MenuItem>
        <MenuItem Command="Save"></MenuItem>
        <MenuItem Command="SaveAs"></MenuItem>
        <Separator></Separator>
        <MenuItem Command="Close"></MenuItem>
      </MenuItem>
    </Menu>

    <ToolBarTray Grid.Row="1">
      <ToolBar>
        <Button Command="New">New</Button>
        <Button Command="Open">Open</Button>
        <Button Command="Save">Save</Button>
      </ToolBar>
      <ToolBar>
        <Button Command="Cut">Cut</Button>
        <Button Command="Copy">Copy</Button>
        <Button Command="Paste">Paste</Button>
      </ToolBar>
    </ToolBarTray>
    <TextBox Margin="5" Grid.Row="2" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>
      </TextBox.CommandBindings>
      
    </TextBox>
    <TextBox Margin="5" Grid.Row="3" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>

        <CommandBinding Command="ApplicationCommands.Save"
          Executed="SaveCommand" />
      </TextBox.CommandBindings>
    </TextBox>
  </Grid>
</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 Commands
{
    public partial class TwoDocument : System.Windows.Window
    {
        public TwoDocument()
        {
        }

        private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {            
            Console.WriteLine("SaveCommand");
        }
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("text changed");
        }
        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }
    }
}

   
     


Format TextBox with MenuItem: normal, bold, italic


   
 

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300">
    <DockPanel LastChildFill="True">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_Edit">
                <MenuItem Header="Cu_t" Command="ApplicationCommands.Cut" />
                <MenuItem Header="_Copy" Command="ApplicationCommands.Copy" />
                <MenuItem Header="_Paste" Command="ApplicationCommands.Paste"/>
                <Separator/>
                <MenuItem Click="SelectAll_Click" Header="_Select All" />
                <MenuItem Click="Clear_Click" Header="_Clear" />
            </MenuItem>
            <MenuItem Header="_Format">
                <MenuItem Click="TextStyle_Click" Header="_Normal" Name="miNormal" />
                <MenuItem Click="TextStyle_Click" FontWeight="Bold" Header="_Bold" Name="miBold" />
                <MenuItem Click="TextStyle_Click" FontStyle="Italic" Header="_Italic" Name="miItalic" />
            </MenuItem>
        </Menu>
        <TextBox Name="txtTextBox" TextWrapping="Wrap">
            text
        </TextBox>
    </DockPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            txtTextBox.Clear();
        }
        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            txtTextBox.SelectAll();
        }
        private void TextStyle_Click(object sender, RoutedEventArgs e)
        {
            if (sender == miNormal)
            {
                txtTextBox.FontWeight = FontWeights.Normal;
                txtTextBox.FontStyle = FontStyles.Normal;
            }
            else if (sender == miBold)
            {
                txtTextBox.FontWeight = FontWeights.Bold;
            }
            else if (sender == miItalic)
            {
                txtTextBox.FontStyle = FontStyles.Italic;
            }
        }
    }
}

   
     


Use MenuItem.Tag to store user object


   
 


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AppWindowsSample" Height="200" Width="300" 
    xmlns:my="clr-namespace:System;assembly=mscorlib">
  <StackPanel>
    <Menu Name="menu1" >
      <MenuItem Header="Window" x:Name="windowMenu">
        <MenuItem Header="SubmenuOpened event" />
      </MenuItem>
    </Menu>
    <Button Margin="10" Name="newWindowButton" Height="23" VerticalAlignment="Top">New Window</Button>
    <StatusBar Grid.Row="2" Name="statusBar1" >
      <TextBlock x:Name="statusText">Status</TextBlock>
    </StatusBar>
    <Button Height="23" Margin="10" Name="shutdownButton" VerticalAlignment="Top" >Shutdown</Button>
  </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;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using System.ComponentModel;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();

      DataContext = Application.Current.Windows;
      statusText.Text = Application.Current.ShutdownMode.ToString();

      newWindowButton.Click += new RoutedEventHandler(newWindowButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
      windowMenu.SubmenuOpened += new RoutedEventHandler(windowMenu_SubmenuOpened);
      Closing += Window1_Closing;
    }

    public void windowMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
      windowMenu.Items.Clear();
      foreach( Window window in Application.Current.Windows ) {
        MenuItem item = new MenuItem();
        item.Header = window.Title;
        item.Click += windowMenuItem_Click;
        item.Tag = window;
        item.IsChecked = window.IsActive;
        windowMenu.Items.Add(item);
      }
    }

    public void windowMenuItem_Click(object sender, RoutedEventArgs e) {
      Window window = (Window)((MenuItem)sender).Tag;
      window.Activate();
    }

    public void newWindowButton_Click(object sender, RoutedEventArgs e) {
      Window window = new Window();
      window.Title = "Window " + DateTime.Now;
      window.Width = 200;
      window.Height = 100;
      window.Show();
      statusText.Text = Application.Current.ShutdownMode.ToString();
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown();
    }

    void Window1_Closing(object sender, CancelEventArgs e) {
      if( MessageBox.Show("Do you really want to shut down?", "Shutting Down", MessageBoxButton.YesNo) == MessageBoxResult.No ) {
        e.Cancel = true;
      }
    }


  }
}

   
     


MenuItem opened event


   
 


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AppWindowsSample" Height="200" Width="300" 
    xmlns:my="clr-namespace:System;assembly=mscorlib">
  <StackPanel>
    <Menu Name="menu1" >
      <MenuItem Header="Window" x:Name="windowMenu">
        <MenuItem Header="SubmenuOpened event" />
      </MenuItem>
    </Menu>
    <Button Margin="10" Name="newWindowButton" Height="23" VerticalAlignment="Top">New Window</Button>
    <StatusBar Grid.Row="2" Name="statusBar1" >
      <TextBlock x:Name="statusText">Status</TextBlock>
    </StatusBar>
    <Button Height="23" Margin="10" Name="shutdownButton" VerticalAlignment="Top" >Shutdown</Button>
  </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;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using System.ComponentModel;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();

      DataContext = Application.Current.Windows;
      statusText.Text = Application.Current.ShutdownMode.ToString();

      newWindowButton.Click += new RoutedEventHandler(newWindowButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
      windowMenu.SubmenuOpened += new RoutedEventHandler(windowMenu_SubmenuOpened);
      Closing += Window1_Closing;
    }

    public void windowMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
      windowMenu.Items.Clear();
      foreach( Window window in Application.Current.Windows ) {
        MenuItem item = new MenuItem();
        item.Header = window.Title;
        item.Click += windowMenuItem_Click;
        item.Tag = window;
        item.IsChecked = window.IsActive;
        windowMenu.Items.Add(item);
      }
    }

    public void windowMenuItem_Click(object sender, RoutedEventArgs e) {
      Window window = (Window)((MenuItem)sender).Tag;
      window.Activate();
    }

    public void newWindowButton_Click(object sender, RoutedEventArgs e) {
      Window window = new Window();
      window.Title = "Window " + DateTime.Now;
      window.Width = 200;
      window.Height = 100;
      window.Show();
      statusText.Text = Application.Current.ShutdownMode.ToString();
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown();
    }

    void Window1_Closing(object sender, CancelEventArgs e) {
      if( MessageBox.Show("Do you really want to shut down?", "Shutting Down", MessageBoxButton.YesNo) == MessageBoxResult.No ) {
        e.Cancel = true;
      }
    }


  }
}

   
     


Add MenuItem to Menu


   
 


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AppWindowsSample" Height="200" Width="300" 
    xmlns:my="clr-namespace:System;assembly=mscorlib">
  <StackPanel>
    <Menu Name="menu1" >
      <MenuItem Header="Window" x:Name="windowMenu">
        <MenuItem Header="SubmenuOpened event" />
      </MenuItem>
    </Menu>
    <Button Margin="10" Name="newWindowButton" Height="23" VerticalAlignment="Top">New Window</Button>
    <StatusBar Grid.Row="2" Name="statusBar1" >
      <TextBlock x:Name="statusText">Status</TextBlock>
    </StatusBar>
    <Button Height="23" Margin="10" Name="shutdownButton" VerticalAlignment="Top" >Shutdown</Button>
  </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;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using System.ComponentModel;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();

      DataContext = Application.Current.Windows;
      statusText.Text = Application.Current.ShutdownMode.ToString();

      newWindowButton.Click += new RoutedEventHandler(newWindowButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
      windowMenu.SubmenuOpened += new RoutedEventHandler(windowMenu_SubmenuOpened);
      Closing += Window1_Closing;
    }

    public void windowMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
      windowMenu.Items.Clear();
      foreach( Window window in Application.Current.Windows ) {
        MenuItem item = new MenuItem();
        item.Header = window.Title;
        item.Click += windowMenuItem_Click;
        item.Tag = window;
        item.IsChecked = window.IsActive;
        windowMenu.Items.Add(item);
      }
    }

    public void windowMenuItem_Click(object sender, RoutedEventArgs e) {
      Window window = (Window)((MenuItem)sender).Tag;
      window.Activate();
    }

    public void newWindowButton_Click(object sender, RoutedEventArgs e) {
      Window window = new Window();
      window.Title = "Window " + DateTime.Now;
      window.Width = 200;
      window.Height = 100;
      window.Show();
      statusText.Text = Application.Current.ShutdownMode.ToString();
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown();
    }

    void Window1_Closing(object sender, CancelEventArgs e) {
      if( MessageBox.Show("Do you really want to shut down?", "Shutting Down", MessageBoxButton.YesNo) == MessageBoxResult.No ) {
        e.Cancel = true;
      }
    }


  }
}

   
     


MenuItem Commands And Events


   
 

<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;
        }
    }
}

   
     


Window with Menu, ToolBar, StatusBar


   
 
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WpfApplication1" Height="400" Width="500"
    WindowStartupLocation ="CenterScreen" >
  <DockPanel>
    <Menu DockPanel.Dock ="Top" HorizontalAlignment="Left" Background="White" BorderBrush ="Black">
      <MenuItem Header="_File" Click ="FileExit_Click" >
        <Separator/>
        <MenuItem Header ="_Exit" MouseEnter ="MouseEnterExitArea" MouseLeave ="MouseLeaveArea" Click ="FileExit_Click"/>
      </MenuItem>
      <MenuItem Header="_Edit">
        <MenuItem Command ="ApplicationCommands.Copy"/>
        <MenuItem Command ="ApplicationCommands.Cut"/>
        <MenuItem Command ="ApplicationCommands.Paste"/>
      </MenuItem>
      <MenuItem Header="_Tools">
        <MenuItem Header ="_Spelling Hints" MouseEnter ="MouseEnterToolsHintsArea" MouseLeave ="MouseLeaveArea" Click ="ToolsSpellingHints_Click"/>
      </MenuItem>
    </Menu>
    <ToolBar DockPanel.Dock ="Top" >
      <Button Content ="Exit" MouseEnter ="MouseEnterExitArea"
                    MouseLeave ="MouseLeaveArea" Click ="FileExit_Click"/>
      <Separator/>
      <Button Content ="Check" MouseEnter ="MouseEnterToolsHintsArea"
                    MouseLeave ="MouseLeaveArea" Click ="ToolsSpellingHints_Click"
                    Cursor="Help" />
    </ToolBar>
    <StatusBar DockPanel.Dock ="Bottom" Background="Beige" >
      <StatusBarItem>
        <TextBlock Name="statBarText">Ready</TextBlock>
      </StatusBarItem>
    </StatusBar>
    <Grid DockPanel.Dock ="Left">
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <GridSplitter Grid.Column ="0" Width ="5" Background ="Gray" />
      <StackPanel Grid.Column="0" VerticalAlignment ="Stretch" >
        <Label Name="lblSpellingInstructions" Margin="10,10,0,0">Spelling Hints</Label>
        <Expander Name="expanderSpelling" Header ="Try these!" Margin="10,10,10,10">
          <Label Name ="lblSpellingHints"/>
        </Expander>
      </StackPanel>
      <TextBox Grid.Column ="1"
          SpellCheck.IsEnabled ="True"
          AcceptsReturn ="True"
          Name ="txtData">
      </TextBox>
    </Grid>
  </DockPanel>
</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 WpfApplication1
{
  public partial class MainWindow : System.Windows.Window
  {
    public MainWindow()
    {
      InitializeComponent();
      SetF1CommandBinding();
    }
    protected void FileExit_Click(object sender, RoutedEventArgs args)
    {
      Application.Current.Shutdown();
    }
    protected void ToolsSpellingHints_Click(object sender, RoutedEventArgs args)
    {
      string spellingHints = string.Empty;
      SpellingError error = txtData.GetSpellingError(txtData.CaretIndex);
      if (error != null)
      {
        foreach (string s in error.Suggestions)
        {
          spellingHints += string.Format("{0}
", s);
        }
        lblSpellingHints.Content = spellingHints;
        expanderSpelling.IsExpanded = true;
      }
    }
    protected void MouseEnterExitArea(object sender, RoutedEventArgs args)
    {
      statBarText.Text = "Exit the Application";
    }
    protected void MouseEnterToolsHintsArea(object sender, RoutedEventArgs args)
    {
      statBarText.Text = "Spelling Suggestions";
    }
    protected void MouseLeaveArea(object sender, RoutedEventArgs args)
    {
      statBarText.Text = "Ready";
    }
    private void SetF1CommandBinding()
    {
      CommandBinding helpBinding = new CommandBinding(ApplicationCommands.Help);
      helpBinding.CanExecute += CanHelpExecute;
      helpBinding.Executed += HelpExecuted;
      CommandBindings.Add(helpBinding);
    }

    private void CanHelpExecute(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }
    private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
    {
      MessageBox.Show("type something!", "Help!");
    }
  }
}