diff --git a/KEY/TouchKeyboard.xaml.cs b/KEY/TouchKeyboard.xaml.cs index 6a17a74..2239905 100644 --- a/KEY/TouchKeyboard.xaml.cs +++ b/KEY/TouchKeyboard.xaml.cs @@ -11,6 +11,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; +using static DyeingComputer.KEY.TouchKeyboardNumeral; namespace DyeingComputer.KEY { @@ -20,8 +21,10 @@ namespace DyeingComputer.KEY public partial class TouchKeyboard : Window { private bool isShiftActive = false; - private bool isSymbolActive = false; public string KeyValue; + public event EventHandler KeyPressed; + private KeyboardMode currentMode = KeyboardMode.Default; + private TextBox targetTextBox; public TouchKeyboard() { @@ -55,19 +58,7 @@ namespace DyeingComputer.KEY { isShiftActive = !isShiftActive; UpdateButtonAppearance(); - } - - private void Symbol_Click(object sender, RoutedEventArgs e) - { - isSymbolActive = !isSymbolActive; - // 这里可以添加切换符号键盘的逻辑 - StatusTextBlock.Text = isSymbolActive ? "符号模式" : "字母模式"; - } - - private void Space_Click(object sender, RoutedEventArgs e) - { - PreviewTextBox.Text += " "; - } + } private void Backspace_Click(object sender, RoutedEventArgs e) { @@ -79,13 +70,16 @@ namespace DyeingComputer.KEY private void Enter_Click(object sender, RoutedEventArgs e) { - KeyValue = PreviewTextBox.Text; + // 触发回车事件 + KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Enter, PreviewTextBox.Text)); + this.Close(); // PreviewTextBox.Text += Environment.NewLine; } private void Hide_Click(object sender, RoutedEventArgs e) { + KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Hide, "")); this.Close(); } @@ -101,5 +95,24 @@ namespace DyeingComputer.KEY ShiftButton.Background = new SolidColorBrush(Color.FromRgb(224, 224, 224)); } } + + public class KeyPressedEventArgs : EventArgs + { + public KeyType KeyType { get; } + public string KeyValue { get; } + + public KeyPressedEventArgs(KeyType keyType, string keyValue) + { + KeyType = keyType; + KeyValue = keyValue; + } + } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + // 初始化为默认模式 + // SetKeyboardMode(KeyboardMode.Default); + PreviewTextBox.Text = null; + } } + } diff --git a/KEY/TouchKeyboardNumeral.xaml.cs b/KEY/TouchKeyboardNumeral.xaml.cs index f1a6760..8903f79 100644 --- a/KEY/TouchKeyboardNumeral.xaml.cs +++ b/KEY/TouchKeyboardNumeral.xaml.cs @@ -19,8 +19,6 @@ namespace DyeingComputer.KEY /// public partial class TouchKeyboardNumeral : Window { - private bool isShiftActive = false; - private bool isSymbolActive = false; private bool mode = false; public string KeyValue; public event EventHandler KeyPressed; diff --git a/View/InOutView.xaml b/View/InOutView.xaml index 1604027..4810fdc 100644 --- a/View/InOutView.xaml +++ b/View/InOutView.xaml @@ -19,7 +19,7 @@ - - datagrid_v = new Dictionary();//缓存函数 - datagrid_v.Add("PLC", newValue); - // SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 - // SQLiteHelpers.Open(); //打开数据库 - MainWindow.SQLiteHelpers.Update("IOName", datagrid_v, "ID ='" + ID + "'", null);//更新 - //SQLiteHelpers.Close();//关闭数据库 + numeralKeyboard = new TouchKeyboardNumeral(); + numeralKeyboard.KeyPressed += NumeralKeyboard_KeyPressed; + numeralKeyboard.Closed += (s, args) => { numeralKeyboard = null; }; } + // 显示键盘 + numeralKeyboard.Show(); + // 定位键盘位置(在主窗口下方) + UpdateKeyboardPosition(); + } + private void Grid_D_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) + { + string newValue = (e.EditingElement as TextBox).Text;//获得输入单元格信息 + Dictionary datagrid_v = new Dictionary();//缓存函数 + datagrid_v.Add("PLC", newValue); + // SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 + // SQLiteHelpers.Open(); //打开数据库 + MainWindow.SQLiteHelpers.Update("IOName", datagrid_v, "ID ='" + ID + "'", null);//更新 + //SQLiteHelpers.Close();//关闭数据库 MainWindowViewModel.SQL_data();//重新获得io表 - MainWindowViewModel.D_view = true; + MainWindowViewModel.D_view = true; } - private void Grid_D_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) + private void Grid_A_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) { - MainWindowViewModel.D_view = false; + MainWindowViewModel.A_view = false; + } + private void Grid_A_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e) + { + currentTextBox = e.EditingElement as TextBox; + if (numeralKeyboard == null) + { + numeralKeyboard = new TouchKeyboardNumeral(); + numeralKeyboard.KeyPressed += NumeralKeyboard_KeyPressed; + numeralKeyboard.Closed += (s, args) => { numeralKeyboard = null; }; + } + // 显示键盘 + numeralKeyboard.Show(); + // 定位键盘位置(在主窗口下方) + UpdateKeyboardPosition(); } - private void Grid_A_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { - string ID; string newValue = (e.EditingElement as TextBox).Text;//获得输入单元格信息 + Dictionary datagrid_v = new Dictionary();//缓存函数 + datagrid_v.Add("PLC", newValue); + //SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 + //SQLiteHelpers.Open(); //打开数据库 + MainWindow.SQLiteHelpers.Update("IOName", datagrid_v, "ID ='" + ID + "'", null);//更新 + //SQLiteHelpers.Close();//关闭数据库 - int rownum = Grid_A.SelectedIndex;//获取鼠标选中行并定义变量 - if (rownum != -1)//判断鼠标定位是否有效 - { - ID = (Grid_A.Columns[0].GetCellContent(Grid_A.Items[rownum]) as TextBlock).Text;//定位第0列, - - Dictionary datagrid_v = new Dictionary();//缓存函数 - datagrid_v.Add("PLC", newValue); - //SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 - //SQLiteHelpers.Open(); //打开数据库 - MainWindow.SQLiteHelpers.Update("IOName", datagrid_v, "ID ='" + ID + "'", null);//更新 - //SQLiteHelpers.Close();//关闭数据库 - } MainWindowViewModel.SQL_data();//重新获得io表 MainWindowViewModel.A_view = true; } - private void Grid_A_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) + // 更新键盘位置 + private void UpdateKeyboardPosition() { - MainWindowViewModel.A_view = false; + if (numeralKeyboard == null) return; + + // 获取主窗口的位置 + var mainWindowWidth = this.Width; + var mainWindowHeight = this.Height; + + // 计算键盘位置(主窗口底部中央) + numeralKeyboard.Left = (mainWindowWidth - numeralKeyboard.Width) / 2; + numeralKeyboard.Top = mainWindowHeight / 2; } + // 键盘按键事件处理 + private void NumeralKeyboard_KeyPressed(object sender, TouchKeyboardNumeral.KeyPressedEventArgs e) + { + switch (e.KeyType) + { + /* case KeyType.Character: + currentTextBox.Text += e.KeyValue; + break; + case KeyType.Backspace: + if (currentTextBox.Text.Length > 0) + { + currentTextBox.Text = currentTextBox.Text.Substring(0, currentTextBox.Text.Length - 1); + } + break;*/ + case KeyType.Enter: + // 回车键处理 + if (!String.IsNullOrEmpty(e.KeyValue)) + { + currentTextBox.Text = e.KeyValue; + } + numeralKeyboard.Close(); + break; + case KeyType.Hide: + // 隐藏键盘处理 + numeralKeyboard.Close(); + break; + } + Grid_D.CommitEdit(); + + Grid_A.CommitEdit(); + } + + } } diff --git a/View/ParameterSetView.xaml b/View/ParameterSetView.xaml index 55b08f2..361a454 100644 --- a/View/ParameterSetView.xaml +++ b/View/ParameterSetView.xaml @@ -15,7 +15,9 @@ BorderThickness="1,1,1,1" ColumnHeaderHeight="40" HorizontalContentAlignment="Right" Grid.ColumnSpan="2" AllowDrop="False" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility ="Column" InputMethod.IsInputMethodEnabled ="False" - CellEditEnding="Grid_CellEditEnding" Background="White" SelectionMode="Single" FontSize="15" Cursor="AppStarting" > + Background="White" SelectionMode="Single" FontSize="15" Cursor="AppStarting" + CellEditEnding="Grid_CellEditEnding" PreparingCellForEdit="Grid_PreparingCellForEdit" + SelectionChanged="Grid_SelectionChanged">