The WPF Notepad Project: Word Wrap, Status Bar, and Zoom (Premium)

It’s time to start building out the Format and View menus and the commands they contain. Then, we’ll implement word-wrap and status bar toggling and wrap up with a truly complex topic: Implementing Zoom using commands.
Format menu
Let’s start with the Format menu. This contains two menu items: Font, which will require a custom dialog, and Word Wrap, which is easy to implement. So we’ll build both menus in XAML and then implement Word Wrap. (You may recall, too, that we have some commented-out lines of code that related to Word Wrap from last time, so we’ll get those going too.)

To do this, open MainWindow.xaml and locate the tag for Format menu item (<MenuItem Header="F_ormat">). Inside of its opening and closing tags, add the two menu items it contains:
<MenuItem Name="FontMenu" Header="_Font" />
<MenuItem Name="WordWrapMenu" Header="_Word Wrap" />
Since neither of these items is associated with a WPF command, we can use simpler Click event handlers in each case. So select the FontMenu item, display its events in the Properties pane and then double-click next to Click to create the following empty FontMenu_Click event handler in MainWindow.xaml.cs:
private void FontMenu_Click(object sender, RoutedEventArgs e)
{

}
Then, do the same for WordWrapMenu:
private void WordWrapMenu_Click(object sender, RoutedEventArgs e)
{

}
For now, we’ll leave FontMenu_Click empty and focus on WordWrapMenu_Click. The code you will enter below checks whether the textbox’s TextWrapping property is set to NoWrap and, if so, changes it to Wrap. Otherwise, it will perform the reverse operation. And in either case, it will check (or not check) the Word Wrap menu item accordingly.
if (TextBox1.TextWrapping == TextWrapping.NoWrap)
{
    TextBox1.TextWrapping = TextWrapping.Wrap;
    WordWrapMenu.IsChecked = true;
}
else
{
    TextBox1.TextWrapping = TextWrapping.NoWrap;
    WordWrapMenu.IsChecked = false;
}
With that simple code completed, we need to uncomment the references to WordWrapMenu in AppWindow_Initialized and SaveSettings. There are two in the former and one in the latter. When done, Word Wrap will work, and .NETpad will correctly load and save this setting.

View menu
The View menu contains three menu items: Theme (which has its own submenu), Zoom, which also has its own submenu, and Status Bar. We’ll build each of these menus in XAML and then implement Status Bar first, since it’s simple (and works much like Word Wrap).

To do so, open MainWindow.xaml and locate the tag for View menu item (<MenuItem Header="_View">). Inside of its opening and closing tags, add the following XAML code for its submenus:
<MenuItem Header="_View">
    <MenuItem Header="_Theme">
        <MenuItem Name="BlackOnWhiteMenu" Header="Black on _White (Default)" />
        <MenuItem Name="BlackOnLightGrayMenu" Header="Black on _Light Gray" />
      �...

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