The WPF Notepad Project: Exit, Print, and More (Premium)

In part two of this project, we’ll add Exit and Print commands, implement several textbox settings, and use the real application name.

Let’s dive right in.
File menu
Let’s build out the File menu so we can start coding the first event handlers. Or, in many cases, the “command handlers,” as Microsoft calls them. We’ll get to that.

Adding sub-menu items to the File menu is simple enough: We just place more menu items (and two separators) inside of the beginning and ending File menu tags in MainWindow.xaml. Like so:
<MenuItem Header="_File">
    <MenuItem Name="NewMenu" Header="_New" />
    <MenuItem Name="NewWindowMenu" Header="New _Window" />
    <MenuItem Name="OpenMenu" Header="_Open..." />
    <MenuItem Name="SaveMenu" Header="_Save" />
    <MenuItem Name="SaveAsMenu" Header="Save _As..." />
    <MenuItem Name="AutoSaveMenu" Header="Auto Sa_ve" />
    <Separator />
    <MenuItem Name="PrintMenu" Header="_Print" />
    <Separator />
    <MenuItem Name="ExitMenu" Header="E_xit" />
</MenuItem>
File > Exit
Exit is probably the simplest command to implement first. And yes, it is possible---and easy---to add a Click event handler to it: Just make sure the Exit menu item is selected in the XAML and then find the Click event in the Properties pane. If you created such a thing---don’t, we’re going in a different direction---it would look like so:
private void ExitMenu_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Shutdown();
}
Better still, it would work. In addition to actually shutting down the application as promised, it would first fire that Close event handler for the window, which would in turn fire the SaveSettings method that saves our settings. It would work fine.

But WPF offers support for a native application Close command. And we can bind that command to a particular method. It’s the WPF way.

To accomplish this, we need to do three things: Add a Command property to the object, in this case the Exit menu item, in XAML; add a command binding to a Windows.CommandBindings section inside of the Window section in XAML that associates the command with an appropriate method; and create that method in C#. For a built-in command like Close, this is as simple as it gets.

So let’s step through that.

First, the command property. Edit the Exit menu’s XAML to include the command. It should look like so when you’re done. (I always put the Command property first so that I can easily parse which objects have commands when I’m viewing MainWindow.xaml.)
<MenuItem Command="Close" Name="ExitMenu" Header="E_xit" />
(The fully qualified name for Close is ApplicationCommands.Close, but Close works fine as-is.)

Next, the command binding. Because this is the first command we’re adding, we will have to create a Windows.CommandBindings section inside of the Window ...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC