浏览代码

Allow movement to after last char with arrow keys.

Fixes #716.
Steven Kirk 9 年之前
父节点
当前提交
6e7fb76966
共有 2 个文件被更改,包括 24 次插入1 次删除
  1. 6 1
      src/Avalonia.Controls/TextBox.cs
  2. 18 0
      tests/Avalonia.Controls.UnitTests/TextBoxTests.cs

+ 6 - 1
src/Avalonia.Controls/TextBox.cs

@@ -575,10 +575,15 @@ namespace Avalonia.Controls
             {
             {
                 var index = caretIndex + direction;
                 var index = caretIndex + direction;
 
 
-                if (index < 0 || index >= text.Length)
+                if (index < 0 || index > text.Length)
                 {
                 {
                     return;
                     return;
                 }
                 }
+                else if (index == text.Length)
+                {
+                    CaretIndex = index;
+                    return;
+                }
 
 
                 var c = text[index];
                 var c = text[index];
 
 

+ 18 - 0
tests/Avalonia.Controls.UnitTests/TextBoxTests.cs

@@ -23,6 +23,24 @@ namespace Avalonia.Controls.UnitTests
                 TextBox.TextProperty.GetMetadata(typeof(TextBox)).DefaultBindingMode);
                 TextBox.TextProperty.GetMetadata(typeof(TextBox)).DefaultBindingMode);
         }
         }
 
 
+        [Fact]
+        public void CaretIndex_Can_Moved_To_Position_After_The_End_Of_Text_With_Arrow_Key()
+        {
+            using (UnitTestApplication.Start(Services))
+            {
+                var target = new TextBox
+                {
+                    Template = CreateTemplate(),
+                    Text = "1234"
+                };
+
+                target.CaretIndex = 3;
+                RaiseKeyEvent(target, Key.Right, 0);
+
+                Assert.Equal(4, target.CaretIndex);
+            }
+        }
+
         [Fact]
         [Fact]
         public void Typing_Beginning_With_0_Should_Not_Modify_Text_When_Bound_To_Int()
         public void Typing_Beginning_With_0_Should_Not_Modify_Text_When_Bound_To_Int()
         {
         {