WPF (Windows Presentation Foundation) is a powerful and flexible framework for developing desktop applications. One of the attractions of WPF is that it easily separates the visual elements of the UI from the backend logic. In this article, we will take a detailed look at the process of creating a basic text editor using WPF.
Overview of WPF
WPF is part of the .NET Framework and is a platform for developing Windows-based applications. WPF uses XAML (Extensible Application Markup Language) to design the UI and can manage data and UI state more easily through the MVVM (Model-View-ViewModel) pattern.
Functional Requirements of the Editor Application
- Text input and editing capabilities
- Basic file open/save functions
- Font and text formatting changes
- Simple search functionality
- Undo/Redo functionality
Project Setup
Open Visual Studio and create a new WPF application project. Set the project name to SimpleTextEditor.
UI Design with XAML
In WPF, the UI is defined through XAML files. Open the MainWindow.xaml file and enter the following code to set up the basic UI.
<Window x:Class="SimpleTextEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Simple Text Editor" Height="450" Width="800">
<Grid>
<Menu VerticalAlignment="Top">
<MenuItem Header="File">
<MenuItem Header="Open" Click="Open_Click" />
<MenuItem Header="Save" Click="Save_Click" />
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Undo" Click="Undo_Click" />
<MenuItem Header="Redo" Click="Redo_Click" />
</MenuItem>
</Menu>
<TextBox x:Name="textBox" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
FontSize="14" VerticalAlignment="Stretch" Margin="0,25,0,0" />
</Grid>
</Window>
WPF Code Behind Implementation
Now, let’s go to the MainWindow.xaml.cs file and implement the basic functionality.
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
namespace SimpleTextEditor
{
public partial class MainWindow : Window
{
private string currentFile;
private bool isDirty;
public MainWindow()
{
InitializeComponent();
currentFile = string.Empty;
isDirty = false;
textBox.TextChanged += (s, e) => { isDirty = true; };
}
private void Open_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
currentFile = openFileDialog.FileName;
textBox.Text = File.ReadAllText(currentFile);
isDirty = false;
Title = "Simple Text Editor - " + currentFile;
}
}
private void Save_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(currentFile))
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog() == true)
{
currentFile = saveFileDialog.FileName;
}
}
if (!string.IsNullOrEmpty(currentFile))
{
File.WriteAllText(currentFile, textBox.Text);
isDirty = false;
Title = "Simple Text Editor - " + currentFile;
}
}
private void Undo_Click(object sender, RoutedEventArgs e)
{
// Simple Undo implementation - use Stack for this purpose
// UndoStack.Push(textBox.Text);
// textBox.Text = UndoStack.Pop();
}
private void Redo_Click(object sender, RoutedEventArgs e)
{
// Simple Redo implementation - use Stack for this purpose
// RedoStack.Push(textBox.Text);
// textBox.Text = RedoStack.Pop();
}
}
}
Feature Description
- Open_Click: Loads the content of the selected file into the text box. The path of the opened file is stored in the
currentFile
variable. - Save_Click: Saves the current content to a file. If a file name is not specified, a dialog box appears for the user to select a file name to save.
- Undo_Click: This part prepares to revert the current text box state to its previous state. By implementing a Stack structure, you can fully implement the Undo and Redo functionalities.
Additional Feature: Change Font and Text Formatting
For a simple editor, you can also add functionality to change the font and text formatting. Let’s add the following code.
<MenuItem Header="Format">
<MenuItem Header="Font" Click="ChangeFont_Click" />
</MenuItem>
Then, add the following method in the code behind.
private void ChangeFont_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog();
if (fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textBox.FontFamily = new FontFamily(fontDialog.Font.Name);
textBox.FontSize = fontDialog.Font.Size;
}
}
Conclusion
In this article, we described how to create a basic text editor using WPF. We designed a simple UI and implemented file handling and basic editing functionalities. Of course, more features and complex requirements can be added, but we focused on the core parts here.
If you have any features or improvements you would like to add to this editor in the future, you can expand upon this foundation. Thanks to the flexibility of WPF, it will also be easier to create more complex applications.
Project Source Code: The complete source code can be found in the GitHub repository.