Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a couple of solutions for this. Perhaps the easiest would be to implement a custom Behavior (you'll need to reference System.Windows.Interactivity.dll) &amp; hook onto some events TextBox exposes, like GotFocus and LostFocus. Here's a sample I've done for selecting all of a TextBox's text when it gets focused:</p> <pre><code>using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interactivity; namespace TextBoxUtility { public class TextBoxSelectAllOnFocusBehavior : Behavior&lt;TextBox&gt; { protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown; this.AssociatedObject.GotKeyboardFocus += OnSelectAll; this.AssociatedObject.MouseDoubleClick += OnSelectAll; } protected override void OnDetaching() { this.AssociatedObject.PreviewMouseLeftButtonDown -= OnPreviewMouseLeftButtonDown; this.AssociatedObject.GotKeyboardFocus -= OnSelectAll; this.AssociatedObject.MouseDoubleClick -= OnSelectAll; base.OnDetaching(); } private static void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var textbox = sender as TextBox; if (textbox != null &amp;&amp; !textbox.IsKeyboardFocusWithin) { textbox.Focus(); e.Handled = true; } } private static void OnSelectAll(object sender, RoutedEventArgs e) { var textbox = sender as TextBox; if (textbox != null) { textbox.SelectAll(); } } </code></pre> <p>Edit: Forgot to show how to hook the behavior up to the TextBox. To attach the Behavior, in your XAML you'll need to add a namespace reference to your Behavior's namespace, and then attach it as a child of the TextBox markup like so:</p> <pre><code>&lt;TextBox Text="{Binding ....}"&gt; &lt;i:Interaction.Behaviors&gt; &lt;utility:TextBoxSelectAllOnFocusBehavior /&gt; &lt;/i:Interaction.Behaviors&gt; &lt;/TextBox&gt; </code></pre> <p>You could do something similar, though with yours you'd want to hook up to the LostFocus-related events and adjust the contents of the TextBox's Text property.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload