Browse Source

其他-调整文本框限制输入逻辑

ZhouJieSheng 1 year ago
parent
commit
443bf4c6c6

+ 5 - 5
PDF Office/ViewModels/BottomToolContentViewModel.cs

@@ -487,11 +487,11 @@ namespace PDF_Master.ViewModels
             }
 
 
-            //枚举 过滤所有不合理的输入
-            if (args.Key != Key.Enter && args.Key != Key.Oem5 && args.Key != Key.Back && args.Key != Key.OemComma && args.Key != Key.OemMinus && args.Key != Key.Subtract && !((args.Key >= Key.D0 && args.Key <= Key.D9) || (args.Key >= Key.NumPad0 && args.Key <= Key.NumPad9)))
-            {
-                args.Handled = true;
-            }
+            ////枚举 过滤所有不合理的输入
+            //if (args.Key != Key.Enter && args.Key != Key.Oem5 && args.Key != Key.Back && args.Key != Key.OemComma && args.Key != Key.OemMinus && args.Key != Key.Subtract && !((args.Key >= Key.D0 && args.Key <= Key.D9) || (args.Key >= Key.NumPad0 && args.Key <= Key.NumPad9)))
+            //{
+            //    args.Handled = true;
+            //}
 
             if (args.Key == Key.Enter)
             {

+ 5 - 5
PDF Office/ViewModels/PageEdit/PageEditContentViewModel.cs

@@ -1064,11 +1064,11 @@ namespace PDF_Master.ViewModels.PageEdit
             }
 
 
-            //枚举 过滤所有不合理的输入
-            if (args.Key != Key.Enter && args.Key != Key.Back && args.Key != Key.OemComma && args.Key != Key.OemMinus && args.Key != Key.Subtract && !((args.Key >= Key.D0 && args.Key <= Key.D9) || (args.Key >= Key.NumPad0 && args.Key <= Key.NumPad9)))
-            {
-                args.Handled = true;
-            }
+            ////枚举 过滤所有不合理的输入
+            //if (args.Key != Key.Enter && args.Key != Key.Back && args.Key != Key.OemComma && args.Key != Key.OemMinus && args.Key != Key.Subtract && !((args.Key >= Key.D0 && args.Key <= Key.D9) || (args.Key >= Key.NumPad0 && args.Key <= Key.NumPad9)))
+            //{
+            //    args.Handled = true;
+            //}
 
             if (args.Key == Key.Enter)
             {

+ 2 - 0
PDF Office/Views/BottomToolContent.xaml

@@ -428,6 +428,8 @@
                             Background="White"
                             BorderThickness="0"
                             CornerRadius="0"
+                            InputMethod.IsInputMethodEnabled="False"
+                            PreviewTextInput="TextBoxEx_PreviewTextInput"
                             ShowClose="False"
                             Style="{StaticResource InnerTextBox}"
                             Text="{Binding CurrentZoom, Mode=OneWay, StringFormat={}{0}%}" />

+ 7 - 0
PDF Office/Views/BottomToolContent.xaml.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
@@ -37,5 +38,11 @@ namespace PDF_Master.Views
             this.SinglePage_Btn.IsChecked = false;
             this.DoublePage_Btn.IsChecked = true;
         }
+
+        private void TextBoxEx_PreviewTextInput(object sender, TextCompositionEventArgs e)
+        {
+            Regex regex = new Regex("[^0-9^%]+"); // 限制仅允许数字和百分号
+            e.Handled = regex.IsMatch(e.Text);
+        }
     }
 }

+ 2 - 0
PDF Office/Views/PageEdit/PageEditContent.xaml

@@ -204,9 +204,11 @@
                             Name="CmbPageRange"
                             Width="117"
                             Height="23"
+                            InputMethod.IsInputMethodEnabled="False"
                             IsEditable="True"
                             ItemsSource="{Binding PageRangeItems}"
                             LostFocus="TextBoxEx_LostFocus"
+                            PreviewTextInput="CmbPageRange_PreviewTextInput"
                             SelectedIndex="{Binding ComboxSelectIndex, Mode=TwoWay}"
                             Text="{Binding TbPageRange, Mode=TwoWay}">
                             <i:Interaction.Triggers>

+ 7 - 0
PDF Office/Views/PageEdit/PageEditContent.xaml.cs

@@ -7,6 +7,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
@@ -1079,5 +1080,11 @@ namespace PDF_Master.Views.PageEdit
                 timer.Tick += Timer_Tick;
             }
         }
+
+        private void CmbPageRange_PreviewTextInput(object sender, TextCompositionEventArgs e)
+        {
+            Regex regex = new Regex("[^0-9,-]+"); // 限制仅允许数字、逗号和横杠
+            e.Handled = regex.IsMatch(e.Text);
+        }
     }
 }