0

I have a TextBox on an MVVM-designed UserControl. I should select all text in the TextBox if some conditions are fulfilled in the ViewModel and also check whether is all text selected.
I have searched a lot how could I implement it, and finally I have tried to use an attached property.

My attached property

public static class TextBoxExtension
{
    public static readonly DependencyProperty IsSelectedAllProperty =
        DependencyProperty.RegisterAttached
        (
            "IsSelectedAll",
            typeof(bool?),
            typeof(TextBoxExtension),
            new FrameworkPropertyMetadata(IsSelectedAllChanged) { BindsTwoWayByDefault = true }
        );
    public static bool? GetIsSelectedAll(DependencyObject element)
    {
        if (element is null)
            throw new ArgumentNullException("element");
        return (bool?)element.GetValue(IsSelectedAllProperty);
    }
    public static void SetIsSelectedAll(DependencyObject element, bool? value)
    {
        if (element is null)
            throw new ArgumentNullException("element");
        element.SetValue(IsSelectedAllProperty, value);
    }
    private static void IsSelectedAllChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBox textbox = (TextBox)d;
        bool? oldValue = e.OldValue as bool?;
        bool? newValue = e.NewValue as bool?;
        if (oldValue is null)
        {
            textbox.GotFocus += SetIsSelectedAllProperty;
            textbox.LostFocus += SetIsSelectedAllProperty;
            textbox.SelectionChanged += SetIsSelectedAllProperty;
        }
        if (newValue == true)
        {
            textbox.Focus();
            textbox.SelectAll();
        }
    }
    private static void SetIsSelectedAllProperty(object sender, RoutedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        textBox.SetValue
        (
            dp: IsSelectedAllProperty,
            value:
                textBox.IsFocused &&
                textBox.SelectedText == textBox.Text
        );
    }
}

My textbox from the View:

<TextBox local:TextBoxExtension.IsSelectedAll="{Binding IsSelectedAll}"/>

It works perfectly in the most cases, but there is a problem when I am trying to select the text from the end to the begin of the text with mouse: when the selection reaches the 1st character the all selection is disappeared immediately.

Why is it happaning and how can I fix it? Please help!

kdani9211
  • 1
  • 2
  • https://stackoverflow.com/a/661224/2470362 – Abin Jan 10 '19 at 22:12
  • Thank you Abin, but it is not related to the handle of the GotFocus event. I have tried to reduce my IsSelectedAllChanged method: I removed the subscribes to the GotFocus and LostFocus events, but the problem is still the same as I wrote in the question. – kdani9211 Jan 10 '19 at 23:25
  • Possible duplicate of [How to automatically select all text on focus in WPF TextBox?](https://stackoverflow.com/questions/660554/how-to-automatically-select-all-text-on-focus-in-wpf-textbox) – Eriawan Kusumawardhono Jan 11 '19 at 05:03

0 Answers0