8

我用这个:

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

但它总是显示0。
我该如何治疗?
谢谢你。

4

4 回答 4

14

我遇到了这个问题(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;
        }
    }
}
于 2009-12-04T20:42:57.967 回答
1

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

于 2009-07-24T07:40:01.743 回答
0

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

阅读本文以获取解决方法。

于 2009-07-24T04:55:58.703 回答
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 组件类型,这是一种耦合,但创建自定义组件也会强制绑定到特定属性。

于 2016-02-20T15:01:44.927 回答