diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index d25649f2a1..680d7e7d2b 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -277,13 +277,15 @@ namespace Avalonia.Controls get { return GetSelection(); } set { - if (value == null) + _undoRedoHelper.Snapshot(); + if (string.IsNullOrEmpty(value)) { - return; + DeleteSelection(); } - - _undoRedoHelper.Snapshot(); - HandleTextInput(value); + else + { + HandleTextInput(value); + } _undoRedoHelper.Snapshot(); } } diff --git a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs index 7f24a57678..6bb3d55799 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs @@ -425,6 +425,42 @@ namespace Avalonia.Controls.UnitTests } } + [Fact] + public void SelectedText_CanClearText() + { + using (UnitTestApplication.Start(Services)) + { + var target = new TextBox + { + Template = CreateTemplate(), + Text = "0123" + }; + target.SelectionStart = 1; + target.SelectionEnd = 3; + target.SelectedText = ""; + + Assert.True(target.Text == "03"); + } + } + + [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() {