Browse Source

SelectedText = null -> clear text

Deadpikle 5 years ago
parent
commit
681911098c

+ 3 - 8
src/Avalonia.Controls/TextBox.cs

@@ -277,19 +277,14 @@ namespace Avalonia.Controls
             get { return GetSelection(); }
             set
             {
-                if (value == null)
-                {
-                    return;
-                }
-
                 _undoRedoHelper.Snapshot();
-                if (value != "")
+                if (string.IsNullOrEmpty(value))
                 {
-                    HandleTextInput(value);
+                    DeleteSelection();
                 }
                 else
                 {
-                    DeleteSelection();
+                    HandleTextInput(value);
                 } 
                 _undoRedoHelper.Snapshot();
             }

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

@@ -443,6 +443,24 @@ namespace Avalonia.Controls.UnitTests
             }
         }
 
+        [Fact]
+        public void SelectedText_NullClearsText()
+        {
+            using (UnitTestApplication.Start(Services))
+            {
+                var target = new TextBox
+                {
+                    Template = CreateTemplate(),
+                    Text = "0123"
+                };
+                target.SelectionStart = 1;
+                target.SelectionEnd = 3;
+                target.SelectedText = null;
+
+                Assert.True(target.Text == "03");
+            }
+        }
+
         [Fact]
         public void CoerceCaretIndex_Doesnt_Cause_Exception_with_malformed_line_ending()
         {