0

I'm trying to bind selectionstart and selectionlength in TextBox by attached properties. Earlier I succeded to bind set focus of this control, but I don't know why selection isn't working (breakpoints in selection methods are never tiggered). I found a few topic connected with this issue (like: How to bind SelectionStart Property of Text Box?), but in answers I see only creating custom control with assinging event of selection changed in ctor. It is possible to achive this without creating custom control? Mine code:

public class TextBoxExtension
{
    public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached(
    "IsFocused", typeof(bool), typeof(TextBoxExtension), new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

    public static readonly DependencyProperty SelectionStartProperty = DependencyProperty.RegisterAttached(
    "SelectionStart", typeof(int), typeof(TextBoxExtension), new PropertyMetadata(OnSelectionStartPropertyChanged));

    public static readonly DependencyProperty SelectionLengthProperty = DependencyProperty.RegisterAttached(
    "SelectionLength", typeof(int), typeof(TextBoxExtension), new PropertyMetadata(OnSelectionLengthPropertyChanged));


    public static bool GetIsFocused(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsFocusedProperty);
    }

    public static void SetIsFocused(DependencyObject obj, bool value)
    {
        obj.SetValue(IsFocusedProperty, value);
    }

    private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (UIElement)d;
        if ((bool)e.NewValue)
        {
            Keyboard.Focus(uie);
            uie.Focus();
        }
    }

    public static int GetSelectionStart(DependencyObject obj)
    {
        return (int)obj.GetValue(SelectionStartProperty);
    }

    public static void SetSelectionStart(DependencyObject obj, int value)
    {
        obj.SetValue(SelectionStartProperty, value);
    }

    private static void OnSelectionStartPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (TextBox)d;
        uie.SelectionStart = (int)e.NewValue;
    }

    public static int GetSelectionLength(DependencyObject obj)
    {
        return (int)obj.GetValue(SelectionLengthProperty);
    }

    public static void SetSelectionLength(DependencyObject obj, int value)
    {
        obj.SetValue(SelectionLengthProperty, value);
    }

    private static void OnSelectionLengthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (TextBox)d;
        uie.SelectionLength = (int)e.NewValue;
    }
}

Xaml:

TextBox x:Name="textBoxSetQuantity" att:TextBoxExtension.SelectionStart="{Binding SelectionStart, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" att:TextBoxExtension.SelectionLength="{Binding SelectionLength, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" att:TextBoxExtension.IsFocused="{Binding TextBoxFocus}"
Elterian
  • 11
  • 4
  • when you attach to the textbox you have to subscribe to the SelectionChanged event and update the attached properties when the event occurs – Sir Rufo Aug 06 '18 at 12:04
  • Subscribe in ctor of TextBoxExtension? Update by adding new method? – Elterian Aug 06 '18 at 13:18

0 Answers0