views:

491

answers:

3

Hello. 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.

A: 

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

Read this article for a work-around solution.

Luke Baulch
This feature had been added in silverlight 3.and works very well in this example: <Slider x:Name="HeightSetterSlider" Maximum="200" Value="100" /> <TextBlock Text="{Binding Value, ElementName=HeightSetterSlider}" />But does not work in mine.I dislike that article, this style actually kills all beauty of binding.
Ivan
+1  A: 

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

Kiril
:) Thank you very much.
Ivan
Is there a way to find out which properties on a given control are DependencyProperties and which are not?
Luke Baulch
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
+3  A: 

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