如何绑定Text Box的SelectionStart属性?

时间:2009-07-24 03:31:42

标签: silverlight binding textbox selection

我用这个:

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

但它总是显示0.
我该怎么对待呢? 谢谢。

4 个答案:

答案 0 :(得分:11)

我遇到了这个问题(SelectionStart和SelectionLength不是依赖属性)并且决定使用可绑定选择开始和结束创建一个TextBox:

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;
        }
    }
}

答案 1 :(得分:1)

您无法绑定到SelectionStart,因为它不是DependencyProperty。

答案 2 :(得分:0)

据我所知,此功能尚未包含在Silverlight 2.0中。

阅读this文章了解解决方案。

答案 3 :(得分:0)

这可能是另一种解决方案:

查看:

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

视图模型:

    #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

我同意你必须在ViewModel中转换TextBox组件类型,它是一种耦合,但是创建一个自定义组件也会强制绑定在特定属性上。