Browse Source

Added AllowBlank parameter to Wpf.TextBoxDialog

frankvandenbos 2 months ago
parent
commit
6a7915fa1f
2 changed files with 16 additions and 4 deletions
  1. 1 1
      inabox.wpf/Editors/TextBoxDialog.xaml
  2. 15 3
      inabox.wpf/Editors/TextBoxDialog.xaml.cs

+ 1 - 1
inabox.wpf/Editors/TextBoxDialog.xaml

@@ -17,7 +17,7 @@
             <RowDefinition Height="Auto" />
         </Grid.RowDefinitions>
         <TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" x:Name="Memo" Background="LightYellow"
-                 AcceptsReturn="True" VerticalScrollBarVisibility="Auto" AcceptsTab="true" />
+                 AcceptsReturn="True" VerticalScrollBarVisibility="Auto" AcceptsTab="true" TextChanged="Memo_OnTextChanged" />
         <Button Grid.Row="1" Grid.Column="1" Margin="5,5,0,0" Width="80" Height="30" x:Name="OK" Content="OK"
                 Click="OK_Click" />
         <Button Grid.Row="1" Grid.Column="2" Margin="5,5,0,0" Width="80" Height="30" x:Name="Cancel" Content="Cancel"

+ 15 - 3
inabox.wpf/Editors/TextBoxDialog.xaml.cs

@@ -1,5 +1,6 @@
 using InABox.Wpf;
 using System.Windows;
+using System.Windows.Controls;
 
 namespace InABox.WPF
 {
@@ -8,11 +9,16 @@ namespace InABox.WPF
     /// </summary>
     public partial class TextBoxDialog : ThemableWindow
     {
-        public TextBoxDialog(string caption, string text)
+
+        private readonly bool _allowblank;
+        
+        public TextBoxDialog(string caption, string text, bool allowblank)
         {
+            _allowblank = allowblank;
             InitializeComponent();
             Title = caption;
             Memo.Text = text;
+            OK.IsEnabled = _allowblank || !string.IsNullOrWhiteSpace(Memo.Text);
         }
 
         public string Text
@@ -20,6 +26,11 @@ namespace InABox.WPF
             get => Memo.Text;
             set => Memo.Text = value;
         }
+        
+        private void Memo_OnTextChanged(object sender, TextChangedEventArgs e)
+        {
+            OK.IsEnabled = _allowblank || !string.IsNullOrWhiteSpace(Memo.Text);
+        }
 
         private void OK_Click(object sender, RoutedEventArgs e)
         {
@@ -33,9 +44,9 @@ namespace InABox.WPF
             Close();
         }
 
-        public static bool Execute(string caption, ref string text)
+        public static bool Execute(string caption, ref string text, bool allowblank = true)
         {
-            var editor = new TextBoxDialog(caption, text);
+            var editor = new TextBoxDialog(caption, text, allowblank);
             if (editor.ShowDialog() == true)
             {
                 text = editor.Memo.Text;
@@ -44,5 +55,6 @@ namespace InABox.WPF
 
             return false;
         }
+
     }
 }