Browse Source

Merge pull request #417 from VitalElement/master

Implemented textbox isreadonly property.
Steven Kirk 9 years ago
parent
commit
06e9160924
1 changed files with 40 additions and 21 deletions
  1. 40 21
      src/Perspex.Controls/TextBox.cs

+ 40 - 21
src/Perspex.Controls/TextBox.cs

@@ -51,6 +51,9 @@ namespace Perspex.Controls
         public static readonly StyledProperty<bool> UseFloatingWatermarkProperty =
             PerspexProperty.Register<TextBox, bool>("UseFloatingWatermark");
 
+        public static readonly StyledProperty<bool> IsReadOnlyProperty =
+            PerspexProperty.Register<TextBox, bool>(nameof(IsReadOnly));
+
         struct UndoRedoState : IEquatable<UndoRedoState>
         {
             public string Text { get; }
@@ -154,6 +157,12 @@ namespace Perspex.Controls
             set { SetValue(UseFloatingWatermarkProperty, value); }
         }
 
+        public bool IsReadOnly
+        {
+            get { return GetValue(IsReadOnlyProperty); }
+            set { SetValue(IsReadOnlyProperty, value); }
+        }
+
         public TextWrapping TextWrapping
         {
             get { return GetValue(TextWrappingProperty); }
@@ -187,17 +196,20 @@ namespace Perspex.Controls
 
         private void HandleTextInput(string input)
         {
-            string text = Text ?? string.Empty;
-            int caretIndex = CaretIndex;
-            if (!string.IsNullOrEmpty(input))
+            if (!IsReadOnly)
             {
-                DeleteSelection();
-                caretIndex = CaretIndex;
-                text = Text ?? string.Empty;
-                Text = text.Substring(0, caretIndex) + input + text.Substring(caretIndex);
-                CaretIndex += input.Length;
-                SelectionStart = SelectionEnd = CaretIndex;
-                _undoRedoHelper.DiscardRedo();
+                string text = Text ?? string.Empty;
+                int caretIndex = CaretIndex;
+                if (!string.IsNullOrEmpty(input))
+                {
+                    DeleteSelection();
+                    caretIndex = CaretIndex;
+                    text = Text ?? string.Empty;
+                    Text = text.Substring(0, caretIndex) + input + text.Substring(caretIndex);
+                    CaretIndex += input.Length;
+                    SelectionStart = SelectionEnd = CaretIndex;
+                    _undoRedoHelper.DiscardRedo();
+                }
             }
         }
 
@@ -510,21 +522,28 @@ namespace Perspex.Controls
 
         private bool DeleteSelection()
         {
-            var selectionStart = SelectionStart;
-            var selectionEnd = SelectionEnd;
-
-            if (selectionStart != selectionEnd)
+            if (!IsReadOnly)
             {
-                var start = Math.Min(selectionStart, selectionEnd);
-                var end = Math.Max(selectionStart, selectionEnd);
-                var text = Text;
-                Text = text.Substring(0, start) + text.Substring(end);
-                SelectionStart = SelectionEnd = CaretIndex = start;
-                return true;
+                var selectionStart = SelectionStart;
+                var selectionEnd = SelectionEnd;
+
+                if (selectionStart != selectionEnd)
+                {
+                    var start = Math.Min(selectionStart, selectionEnd);
+                    var end = Math.Max(selectionStart, selectionEnd);
+                    var text = Text;
+                    Text = text.Substring(0, start) + text.Substring(end);
+                    SelectionStart = SelectionEnd = CaretIndex = start;
+                    return true;
+                }
+                else
+                {
+                    return false;
+                }
             }
             else
             {
-                return false;
+                return true;
             }
         }