WPF Development, Practice Discussion Page Creation

WPF (Windows Presentation Foundation) is a very useful technology for designing and developing Windows applications. In this tutorial, we will learn how to create a simple discussion page using WPF. This tutorial will cover the basic concepts of WPF, along with practical projects utilizing data binding, event handling, custom controls, and the MVVM (Model-View-ViewModel) pattern.

1. What is WPF?

WPF is a UI framework developed by Microsoft, based on the .NET Framework. With WPF, you can easily and quickly create advanced user interfaces, and develop visually appealing applications with vector graphics, styling, and templating features.

2. Key Features of WPF

  • XAML (Extensible Application Markup Language) based UI design
  • Efficient UI work through data binding
  • Improved code separation and maintainability through the MVVM pattern
  • Various multimedia capabilities including 3D graphics and animations
  • Support for effective collaboration between design and development

3. Designing the Discussion Page

In this project, we will design a discussion page with the following features.

  • A text box where users can enter their opinions
  • When the submit button is clicked, the opinion is added to the list
  • A button to delete submitted opinions

4. Project Setup

Open Visual Studio and create a new WPF application project. Set the project name to “DiscussionPage”. You can design the UI in the automatically generated MainWindow.xaml file.

5. XAML UI Design


<Window x:Class="DiscussionPage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Discussion Page" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="CommentTextBox" Width="600" Height="30" Margin="10" PlaceholderText="Enter your opinion..." />
            <Button x:Name="SubmitButton" Content="Submit" Width="80" Height="30" Click="SubmitButton_Click" Margin="10"/>
        </StackPanel>

        <ListBox x:Name="CommentsListBox" Grid.Row="1" Margin="10" />
    </Grid>
</Window>

6. Code Behind

The UI is ready, so now we prepare to write C# code to handle user input. Write the following in the MainWindow.xaml.cs file.


using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace DiscussionPage
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<string> Comments { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            Comments = new ObservableCollection<string>();
            CommentsListBox.ItemsSource = Comments;
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(CommentTextBox.Text))
            {
                Comments.Add(CommentTextBox.Text);
                CommentTextBox.Clear();
            }
            else
            {
                MessageBox.Show("Please enter your opinion.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
    }
}

7. Code Explanation

The most important part of the above code is the use of ObservableCollection to perform data binding with the list box. When the user submits an opinion, the entered opinion is added to the list and reflected in the UI in real-time.

8. Feature Expansion – Adding Delete Functionality

Let’s add a feature to delete the opinions registered by the users. We will add a button to remove the selected item from the list box.


<Button x:Name="DeleteButton" Content="Delete" Width="80" Height="30" Click="DeleteButton_Click" Margin="10"/>

9. Adding Delete Button Event Handler


private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
    string selectedComment = (string)CommentsListBox.SelectedItem;
    if (selectedComment != null)
    {
        Comments.Remove(selectedComment);
    }
    else
    {
        MessageBox.Show("Please select an opinion to delete.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
    }
}

10. Final Check and Execution

All the code and UI are complete. You can now run the project to test the implemented features. When a user enters an opinion and submits it, it will be added to the list, and the selected opinion can be deleted.

11. Additional Feature Considerations

Now that the project has basic functionality, here are some additional features to consider:

  • Comment feature for opinions
  • Like feature for comments
  • Adding a name field for the opinion writer
  • API connections for extension to a web application

12. Conclusion

In this tutorial, we created a simple discussion page using WPF. I hope it served as a great opportunity to learn the basic concepts of WPF, data binding, and the importance of event handling and the MVVM pattern.

I encourage you to expand your skills through more projects and practice with WPF. For example, this project could be improved and changed into a web application.

I look forward to your support and feedback. Good luck!