Browse Source

Allow movement to after last char with arrow keys.

Fixes #716.
Steven Kirk 9 years ago
parent
commit
6e7fb76966

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

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