Universal Windows Phone TextBox.SelectAll () applications with copy enabled not working

In Store Apps / Universal Apps / Windows Phone 8.1 Visual Studio 2013 applications, how to programmatically select all the text in a TextBox with the copy icon context menu turned on, as shown in the following screenshot:

a busy cat
(source: free.fr )

It is necessary to display the text in a context where there is a high probability that the user wants to copy it to the clipboard.

The following tests did not work:

Xaml

<TextBox x:Name="MyTextBox" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="The text I want to select" IsReadOnly="True" IsEnabled="True" GotFocus="MyTextBox_GotFocus"></TextBox> <Button x:Name="ButtonSelectAll" Grid.Row="1" Content="Select All" HorizontalAlignment="Center" Click="ButtonSelectAll_Click"></Button> 

FROM#

  private void ButtonSelectAll_Click(object sender, RoutedEventArgs e) { MyTextBox.SelectAll(); // MyTextBox.Focus(FocusState.Programmatic); } private void MyTextBox_GotFocus(object sender, RoutedEventArgs e) { MyTextBox.SelectAll(); } 

Try the Click event to do nothing. The GotFocus try event selects all the text, but the context menu of the copy icon and two markers are not displayed. If you add "text.Focus (FocusState.Programmatic);" to the Click method then the text is highlighted, but the copy icon is not displayed. And, unfortunately, if you touch this text with your finger with the intention of the appearance of the "copy icon", you will lose the selection.

+5
source share
1 answer

@efdummy Try the following:

  private void ButtonSelectAll_Click(object sender, RoutedEventArgs e) { MyTextBox.Select(0, MyTextBox.Text.Length); string selectedText = MyTextBox.SelectedText; DataPackage myPackage = new DataPackage(); myPackage.SetText(selectedText); Clipboard.SetContent(myPackage); } 
0
source

Source: https://habr.com/ru/post/1206974/


All Articles