如何绑定到CaretIndex也称为文本框的光标位置

时间:2015-01-30 10:30:54

标签: c# wpf mvvm binding textbox

您好我正在尝试绑定到不是TextBox.CaretIndex的{​​{1}}属性,因此我创建了DependencyProperty,但它无法按预期工作。

期望(专注时)

  • 默认= 0
  • 如果我更改视图中的值,则应更改 viewmodel
  • 中的值
  • 如果我更改 viewmodel 中的值,则应更改视图中的值

当前行为

  • viewmodel值在窗口打开时被调用

代码隐藏

Behavior

XAML

public class TextBoxBehavior : DependencyObject
{
    public static readonly DependencyProperty CursorPositionProperty =
        DependencyProperty.Register(
            "CursorPosition",
            typeof(int),
            typeof(TextBoxBehavior),
            new FrameworkPropertyMetadata(
                default(int),
                new PropertyChangedCallback(CursorPositionChanged)));

    public static void SetCursorPosition(DependencyObject dependencyObject, int i)
    {
        // breakpoint get never called
        dependencyObject.SetValue(CursorPositionProperty, i); 
    }

    public static int GetCursorPosition(DependencyObject dependencyObject)
    {
        // breakpoint get never called
        return (int)dependencyObject.GetValue(CursorPositionProperty);
    }

    private static void CursorPositionChanged(
        DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        // breakpoint get never called
        //var textBox = dependencyObject as TextBox;
        //if (textBox == null) return;
    }
}

更多信息

我认为这里存在一些问题,因为我需要从<TextBox Text="{Binding TextTemplate,UpdateSourceTrigger=PropertyChanged}" local:TextBoxBehavior.CursorPosition="{Binding CursorPosition}"/> 派生它,这是以前从未需要的,因为DependencyObject已经是CursorPositionProperty,所以这应该足够了。我还认为我需要在DependencyProperty中使用一些事件来正确设置我的Behavior,但我不知道哪个。

4 个答案:

答案 0 :(得分:6)

与我的行为斗争后,我可以为您呈现 99%工作解决方案

<强>行为

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfMVVMTextBoxCursorPosition
{
    public class TextBoxCursorPositionBehavior : DependencyObject
    {
        public static void SetCursorPosition(DependencyObject dependencyObject, int i)
        {
            dependencyObject.SetValue(CursorPositionProperty, i);
        }

        public static int GetCursorPosition(DependencyObject dependencyObject)
        {
            return (int)dependencyObject.GetValue(CursorPositionProperty);
        }

        public static readonly DependencyProperty CursorPositionProperty =
                                           DependencyProperty.Register("CursorPosition"
                                                                       , typeof(int)
                                                                       , typeof(TextBoxCursorPositionBehavior)
                                                                       , new FrameworkPropertyMetadata(default(int))
                                                                       {
                                                                           BindsTwoWayByDefault = true
                                                                           ,DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                                                                       }
                                                                       );

        public static readonly DependencyProperty TrackCaretIndexProperty =
                                                    DependencyProperty.RegisterAttached(
                                                        "TrackCaretIndex",
                                                        typeof(bool),
                                                        typeof(TextBoxCursorPositionBehavior),
                                                        new UIPropertyMetadata(false
                                                                                , OnTrackCaretIndex));

        public static void SetTrackCaretIndex(DependencyObject dependencyObject, bool i)
        {
            dependencyObject.SetValue(TrackCaretIndexProperty, i);
        }

        public static bool GetTrackCaretIndex(DependencyObject dependencyObject)
        {
            return (bool)dependencyObject.GetValue(TrackCaretIndexProperty);
        }

        private static void OnTrackCaretIndex(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
        {
            var textbox = dependency as TextBox;

            if (textbox == null)
                return;
            bool oldValue = (bool)e.OldValue;
            bool newValue = (bool)e.NewValue;

            if (!oldValue && newValue) // If changed from false to true
            {
                textbox.SelectionChanged += OnSelectionChanged;
            }
            else if (oldValue && !newValue) // If changed from true to false
            {
                textbox.SelectionChanged -= OnSelectionChanged;
            }
        }

        private static void OnSelectionChanged(object sender, RoutedEventArgs e)
        {
            var textbox = sender as TextBox;

            if (textbox != null)
                SetCursorPosition(textbox, textbox.CaretIndex); // dies line does nothing
        }
    }
}

<强> XAML

    <TextBox Height="50" VerticalAlignment="Top"
             Name="TestTextBox"
             Text="{Binding MyText}"
             vm:TextBoxCursorPositionBehavior.TrackCaretIndex="True"
             vm:TextBoxCursorPositionBehavior.CursorPosition="{Binding CursorPosition,Mode=TwoWay}"/>

    <TextBlock Height="50" Text="{Binding CursorPosition}"/>

只是因为我不知道为什么它不起作用=&gt; BindsTwoWayByDefault = true。据我所知,它对绑定没有影响因为我需要在XAML中明确设置绑定模式

答案 1 :(得分:1)

WiiMaxx 的解决方案对我来说有以下问题:

  1. 从代码更改视图模型属性时,文本框中的插入符号索引不会更改。 Tejas Vaishnav 在对解决方案的评论中也提到了这一点。
  2. BindsTwoWayByDefault = true 不起作用。
  3. 他说他需要继承 DependencyObject 很奇怪。
  4. TrackCaretIndex 属性仅用于初始化,感觉有点不必要。

这是我解决这些问题的解决方案:

行为

public static class TextBoxAssist
{

    // This strange default value is on purpose it makes the initialization problem very unlikely.
    // If the default value matches the default value of the property in the ViewModel,
    // the propertyChangedCallback of the FrameworkPropertyMetadata is initially not called
    // and if the property in the ViewModel is not changed it will never be called.
    private const int CaretIndexPropertyDefault = -485609317;

    public static void SetCaretIndex(DependencyObject dependencyObject, int i)
    {
        dependencyObject.SetValue(CaretIndexProperty, i);
    }

    public static int GetCaretIndex(DependencyObject dependencyObject)
    {
        return (int)dependencyObject.GetValue(CaretIndexProperty);
    }

    public static readonly DependencyProperty CaretIndexProperty =
        DependencyProperty.RegisterAttached(
            "CaretIndex",
            typeof(int),
            typeof(TextBoxAssist),
            new FrameworkPropertyMetadata(
                CaretIndexPropertyDefault,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                CaretIndexChanged));

    private static void CaretIndexChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
    {
        if (dependencyObject is not TextBox textBox || eventArgs.OldValue is not int oldValue || eventArgs.NewValue is not int newValue)
        {
            return;
        }

        if (oldValue == CaretIndexPropertyDefault && newValue != CaretIndexPropertyDefault)
        {
            textBox.SelectionChanged += SelectionChangedForCaretIndex;
        }
        else if (oldValue != CaretIndexPropertyDefault && newValue == CaretIndexPropertyDefault)
        {
            textBox.SelectionChanged -= SelectionChangedForCaretIndex;
        }

        if (newValue != textBox.CaretIndex)
        {
            textBox.CaretIndex = newValue;
        }
    }

    private static void SelectionChangedForCaretIndex(object sender, RoutedEventArgs eventArgs)
    {
        if (sender is TextBox textBox)
        {
            SetCaretIndex(textBox, textBox.CaretIndex);
        }
    }

}

XAML

    <TextBox Height="50" VerticalAlignment="Top"
             Name="TestTextBox"
             Text="{Binding MyText}"
             viewModels:TextBoxAssist.CaretIndex="{Binding CaretIndex}"/>

对差异的一些说明:

  • 视图模型属性更改现在起作用了,因为 TextBox 上的插入符号索引设置在 CaretIndexChanged 的末尾。
  • 使用相应的 BindsTwoWayByDefault 构造函数参数修复了 FrameworkPropertyMetadata
  • DependencyObject 继承只是必要的,因为使用了 DependencyProperty.Register 而不是 DependencyProperty.RegisterAttached
  • 如果没有 TrackCaretIndex 属性,我遇到的问题是从未调用 propertyChangedCallbackFrameworkPropertyMetadata 来正确初始化事物。仅当 FrameworkPropertyMetadata 的默认值从一开始就与视图模型属性的值匹配并且视图模型属性未更改时,才会出现问题。这就是我使用这个随机默认值的原因。

答案 2 :(得分:0)

正如您所说,TextBox.CaretIndex Property 一个DependencyProperty,因此您无法将数据绑定到它。即使使用您自己的DependencyProperty,也不会有效......如果TextBox.CaretIndex属性发生变化,您希望如何收到通知?

答案 3 :(得分:0)

我遇到了类似的问题,对我来说最简单的解决方案是从TextBox继承并添加DependencyProperty。所以它看起来像这样:

namespace UI.Controls
{
    public class MyTextBox : TextBox
    {
        public static readonly DependencyProperty CaretPositionProperty =
            DependencyProperty.Register("CaretPosition", typeof(int), typeof(MyTextBox),
                new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCaretPositionChanged));

        public int CaretPosition
        {
            get { return (int)GetValue(CaretPositionProperty); }
            set { SetValue(CaretPositionProperty, value); }
        }

        public MyTextBox()
        {
            SelectionChanged += (s, e) => CaretPosition = CaretIndex;
        }

        private static void OnCaretPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as MyTextBox).CaretIndex = (int)e.NewValue;
        }
    }
}

...在我的XAML中:

xmlns:controls="clr-namespace:IU.Controls"
...
<controls:MyTextBox CaretPosition="{Binding CaretPosition}"/>
当然,在视图模型中

...和CaretPosition属性。如果您不打算将View模型绑定到其他文本编辑控件,这可能就足够了,如果是 - 您可能需要其他解决方案。