The WPF Notepad Project: Save, Save As, New, New Window (Premium)

I originally intended to document Save and Save As in the previous article in this series, but I ended up improving it dramatically, which triggered a big rewrite. As I mentioned previously in The WPF Files series, my original recreation of .NETpad using WPF was done rather quickly, so I’ve been cleaning it up where necessary while documenting how I did it here. And this was one of the bigger fixes.

Anyway, like other document-based applications, .NETpad lets the user save a document (“Save”) or save a document using a different file name (“Save As”). But it’s a little more nuanced then that: If you’re working with a new, as-yet-unsaved document and you choose to save it, you’re really doing a Save As in that one instance; subsequent saves will call Save instead.

WPF provides support for Save and SaveAs commands. But because we will need to use the functionality from either in other contexts, we’ll create our own Save and SaveAs methods and then call them from SaveCommand_Executed and SaveAsCommand_Executed. (And later, from elsewhere too.)

To get started down this path, open MainWindow.xaml and locate the <Window.CommandBindings> section. Add the following two lines of XAML inside this section and between New and Print:
<CommandBinding Command="Save" Executed="SaveCommand_Executed" />
<CommandBinding Command="SaveAs" Executed="SaveAsCommand_Executed" />
Then, locate the Save and Save As menu item tags and add the appropriate Command properties to each so that those two lines read like so:
<MenuItem Command="Save" Name="SaveMenu" Header="_Save" />
<MenuItem Command="SaveAs" Name="SaveAsMenu" Header="Save _As..." InputGestureText="Ctrl+Shift+S" />
Because the SaveAs command doesn’t automatically provide the associated keyboard shortcut (and, in the menu, the shortcut hint), we also need to add a key binding to the <Window.InputBindings> section in the XAML file too. That looks like so:
<KeyBinding Key="S" Modifiers="Ctrl + Shift" Command="SaveAs" />
Over in MainWindow.xaml.cs, we now need to create empty Save() and SaveAs() methods inside of the MainWindow class definition, like so:
private void Save()
{

}

private void SaveAs()
{

}
Then, we need to add Save and Save As command handlers that call those methods:
private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
    Save();
}

private void SaveAsCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
    SaveAs();
}
Let’s code SaveAs first, since the Save method will need to call SaveAs if the current document has never been saved.

SaveAs basically needs to display a Save As dialog. If the user creates a name and chooses to save, our application will create the new text file with that name (in whatever location the user chose), change the DocumentName global variable to that filename, change the application title bar to reflect the filename, and change T...

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