8

I use this:

<TextBox x:Name="Test"/>
<TextBlock Text="{Binding SelectionStart, ElementName=Test}"/>

but it always shows 0.
How can I treat it?
Thank you.

Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
Ivan
  • 1,103
  • 2
  • 10
  • 15

4 Answers4

14

I ran into this problem (SelectionStart and SelectionLength are not dependency properties) and decided to make a TextBox with bindable selection start and end:

public class SelectionBindingTextBox : TextBox
{
    public static readonly DependencyProperty BindableSelectionStartProperty =
        DependencyProperty.Register(
        "BindableSelectionStart",
        typeof(int),
        typeof(SelectionBindingTextBox),
        new PropertyMetadata(OnBindableSelectionStartChanged));

    public static readonly DependencyProperty BindableSelectionLengthProperty =
        DependencyProperty.Register(
        "BindableSelectionLength",
        typeof(int),
        typeof(SelectionBindingTextBox),
        new PropertyMetadata(OnBindableSelectionLengthChanged));

    private bool changeFromUI;

    public SelectionBindingTextBox() : base()
    {
        this.SelectionChanged += this.OnSelectionChanged;
    }

    public int BindableSelectionStart
    {
        get
        {
            return (int)this.GetValue(BindableSelectionStartProperty);
        }

        set
        {
            this.SetValue(BindableSelectionStartProperty, value);
        }
    }

    public int BindableSelectionLength
    {
        get
        {
            return (int)this.GetValue(BindableSelectionLengthProperty);
        }

        set
        {
            this.SetValue(BindableSelectionLengthProperty, value);
        }
    }

    private static void OnBindableSelectionStartChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        var textBox = dependencyObject as SelectionBindingTextBox;

        if (!textBox.changeFromUI)
        {
            int newValue = (int)args.NewValue;
            textBox.SelectionStart = newValue;
        }
        else
        {
            textBox.changeFromUI = false;
        }
    }

    private static void OnBindableSelectionLengthChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        var textBox = dependencyObject as SelectionBindingTextBox;

        if (!textBox.changeFromUI)
        {
            int newValue = (int)args.NewValue;
            textBox.SelectionLength = newValue;
        }
        else
        {
            textBox.changeFromUI = false;
        }
    }

    private void OnSelectionChanged(object sender, RoutedEventArgs e)
    {
        if (this.BindableSelectionStart != this.SelectionStart)
        {
            this.changeFromUI = true;
            this.BindableSelectionStart = this.SelectionStart;
        }

        if (this.BindableSelectionLength != this.SelectionLength)
        {
            this.changeFromUI = true;
            this.BindableSelectionLength = this.SelectionLength;
        }
    }
}
RandomEngy
  • 14,931
  • 5
  • 70
  • 113
  • Seems to be the best solution available but this does not work very well with two way databinding. – Haydn Apr 29 '18 at 02:05
1

You cannot bind to SelectionStart because it is not a DependencyProperty.

Kiril Stanoev
  • 1,865
  • 2
  • 20
  • 33
  • Is there a way to find out which properties on a given control are DependencyProperties and which are not? – Luke Baulch Jul 31 '09 at 02:34
  • The fastest way is to use the Intellisense of Visual Studio. For example, assume you want to see all Dependency Properties of a TextBox. Just type TextBox. and intellisense will show you all of its dependency properties. – Kiril Stanoev Jul 31 '09 at 08:26
0

As far as I am aware, this feature has not been included in Silverlight 2.0.

Read this article for a work-around solution.

Treer
  • 433
  • 4
  • 13
Luke Baulch
  • 3,626
  • 6
  • 36
  • 44
  • This feature had been added in silverlight 3. and works very well in this example: But does not work in mine. I dislike that article, this style actually kills all beauty of binding. – Ivan Jul 24 '09 at 05:12
0

This could be an alternate solution:

View:

<TextBox Text="{Binding Text}">
    <i:Interaction.Triggers>
       <i:EventTrigger EventName="SelectionChanged">
           <mvvml:EventToCommand Command="{Binding TextBoxSelectionChangedCommand}" 
                                 PassEventArgsToCommand="True" />
       </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

ViewModel:

    #region TextBoxSelectionChangedCommand
    RelayCommand<RoutedEventArgs> _TextBoxSelectionChangedCommand = null;
    public ICommand TextBoxSelectionChangedCommand {
        get {
            if (_TextBoxSelectionChangedCommand == null) {
                _TextBoxSelectionChangedCommand = new RelayCommand<RoutedEventArgs>((r) => TextBoxSelectionChanged(r), (r) => true);
            }

            return _TextBoxSelectionChangedCommand;
        }
    }

    protected virtual void TextBoxSelectionChanged(RoutedEventArgs _args) {
        YourCursorPositionVariable = (_args.OriginalSource as System.Windows.Controls.TextBox).SelectionStart;
    }
    #endregion

I agree you has to cast TextBox component type in ViewModel and it's a kind of coupling, but create a custom component will impose to bind on a specific property as well.

tuxy42
  • 368
  • 3
  • 6