UWP - How to use TextBox SelectionChanged event

UWP - TextBox SelectionChanged Event
The TextBox class represents a control that can be used to display and edit plain text. A TextBox can be single or multi-line. The TextBox control enables the UWP app user to enter text into a UWP app. The TextBox is typically used to capture a single line of text but the UWP developers can configure it to capture multiple lines of text. The text displays on the screen in a simple uniform plaintext format.

The following Universal Windows Platform application development tutorial demonstrates how we can use the TextBox SelectionChanged event. Here we displayed the TextBox selected text on a TextBlock control when the TextBox selection changed.

The TextBox SelectionChanged event occurs when the text selection has changed. The TextBox SelectionChanged event type is RoutedEventHandler. The RoutedEventHandler delegate represents the method that will handle routed events.

The RoutedEventHandler(object sender, RoutedEventArgs e) method has two parameters named sender and e. The sender parameter is an Object to which the event handler is attached. And the e parameter is RoutedEventArgs which is the event data.

The TextBox SelectedText property gets or sets the content of the current selection in the text box. This property value is a String which is the currently selected text in the text box. If no text is selected then the value is an empty String.
MainPage.xaml

<Page
    x:Class="UniversalAppTutorials.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UniversalAppTutorials"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    >
    <StackPanel Padding="50" Background="AliceBlue">
        <TextBox
            x:Name="TextBox1"
            PlaceholderText="Input some text here"
            Header="TextBox"
            Margin="5"
            MaxHeight="125"
            Height="100"
            AcceptsReturn="True"
            TextWrapping="Wrap"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            SelectionChanged="TextBox1_SelectionChanged"
            SelectionHighlightColor="Crimson"
            />
        <TextBlock
            x:Name="TextBlock1"
            FontFamily="MV Boli"
            FontSize="30"
            Foreground="DodgerBlue"
            TextWrapping="Wrap"
            Margin="5"
            />
    </StackPanel>
</Page>
MainPage.xaml.cs

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;


namespace UniversalAppTutorials
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /*
            TextBox.SelectionChanged event
                Occurs when the text selection has changed.
        */

        private void TextBox1_SelectionChanged(object sender, RoutedEventArgs e)
        {
            // Cast sender as a TextBox
            TextBox textBox = sender as TextBox;

            if (textBox.SelectedText.Length > 0)
            {
                // Get the TextBox selected text and display it on TextBlock
                TextBlock1.Text = "You selected:\n" + textBox.SelectedText;
            }
            else {
                TextBlock1.Text = "";
            }
        }
    }
}
More uwp tutorials