Browse Source

内置触摸键盘,参数设置系统设置添加打开触摸键盘功能

master
sc 2 months ago
parent
commit
683c7b03f3
  1. 43
      KEY/TouchKeyboard.xaml.cs
  2. 2
      KEY/TouchKeyboardNumeral.xaml.cs
  3. 4
      View/InOutView.xaml
  4. 149
      View/InOutView.xaml.cs
  5. 4
      View/ParameterSetView.xaml
  6. 95
      View/ParameterSetView.xaml.cs
  7. 3
      View/SysSetView.xaml
  8. 93
      View/SysSetView.xaml.cs

43
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<KeyPressedEventArgs> 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;
}
}
}

2
KEY/TouchKeyboardNumeral.xaml.cs

@ -19,8 +19,6 @@ namespace DyeingComputer.KEY
/// </summary>
public partial class TouchKeyboardNumeral : Window
{
private bool isShiftActive = false;
private bool isSymbolActive = false;
private bool mode = false;
public string KeyValue;
public event EventHandler<KeyPressedEventArgs> KeyPressed;

4
View/InOutView.xaml

@ -19,7 +19,7 @@
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="Grid_D" AlternationCount="2" SelectionChanged="Grid_D_SelectionChanged"
<DataGrid x:Name="Grid_D" AlternationCount="2" SelectionChanged="Grid_D_SelectionChanged" PreparingCellForEdit="Grid_D_PreparingCellForEdit"
CellEditEnding="Grid_D_CellEditEnding" BeginningEdit="Grid_D_BeginningEdit" d:ItemsSource="{d:SampleData ItemCount=512}"
Margin="0,0,0,0" ItemsSource="{Binding Path=SYSData_D,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" MinColumnWidth="30"
HorizontalGridLinesBrush="#FFC9C9C9" VerticalGridLinesBrush="#FFC9C9C9" GridLinesVisibility="All" BorderBrush="#CCCCCC"
@ -81,7 +81,7 @@
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="Grid_A" AlternationCount="2" SelectionChanged="Grid_A_SelectionChanged"
<DataGrid x:Name="Grid_A" AlternationCount="2" SelectionChanged="Grid_A_SelectionChanged" PreparingCellForEdit="Grid_A_PreparingCellForEdit"
CellEditEnding="Grid_A_CellEditEnding" BeginningEdit="Grid_A_BeginningEdit" d:ItemsSource="{d:SampleData ItemCount=256}"
Margin="0,0,0,0" ItemsSource="{Binding Path=SYSData_A,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" MinColumnWidth="30"
HorizontalGridLinesBrush="#FFC9C9C9" VerticalGridLinesBrush="#FFC9C9C9" GridLinesVisibility="All" BorderBrush="#CCCCCC"

149
View/InOutView.xaml.cs

@ -1,8 +1,10 @@
using DyeingComputer.ViewModel;
using DyeingComputer.KEY;
using DyeingComputer.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
@ -28,75 +30,140 @@ namespace DyeingComputer.View
{
InitializeComponent();
}
// private SQLiteHelper SQLiteHelpers = null; //定义数据库
string ID;
private TextBox currentTextBox;
private TouchKeyboardNumeral numeralKeyboard;
// private SQLiteHelper SQLiteHelpers = null; //定义数据库
//private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径
private void Grid_D_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int rownum = Grid_D.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
{
ID = (Grid_D.Columns[0].GetCellContent(Grid_D.Items[rownum]) as TextBlock).Text;//定位第0列,
}
}
private void Grid_A_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int rownum = Grid_A.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
{
ID = (Grid_A.Columns[0].GetCellContent(Grid_A.Items[rownum]) as TextBlock).Text;//定位第0列,
}
}
private void Grid_M_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void Grid_D_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
private void Grid_D_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
string ID;
string newValue = (e.EditingElement as TextBox).Text;//获得输入单元格信息
int rownum = Grid_D.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
MainWindowViewModel.D_view = false;
}
private void Grid_D_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
currentTextBox = e.EditingElement as TextBox;
if (numeralKeyboard == null)
{
ID = (Grid_D.Columns[0].GetCellContent(Grid_D.Items[rownum]) as TextBlock).Text;//定位第0列,
Dictionary<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
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<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
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<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
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<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
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();
}
}
}

4
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">
<DataGrid.RowStyle >
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="30" />

95
View/ParameterSetView.xaml.cs

@ -1,4 +1,5 @@
using DyeingComputer.ViewModel;
using DyeingComputer.KEY;
using DyeingComputer.ViewModel;
using System;
using System.Collections;
using System.Collections.Generic;
@ -29,6 +30,9 @@ namespace DyeingComputer.View
/// </summary>
public partial class ParameterSetView : UserControl
{
private TextBox currentTextBox;
string ID;
private TouchKeyboard Keyboard;
public ParameterSetView()
{
InitializeComponent();
@ -50,22 +54,16 @@ namespace DyeingComputer.View
private void Grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)//数据表输入事件
{
string ID;
string newValue = (e.EditingElement as TextBox).Text;//获得输入单元格信息
int rownum = Grid.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
{
ID = (Grid.Columns[1].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第0列,
Dictionary<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
datagrid_v.Add("Value", newValue);
//SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
//SQLiteHelpers.Open(); //打开数据库
MainWindow.SQLiteHelpers.Update("Parameters", datagrid_v, "ParameterID ='" + ID + "'", null);//更新
MainWindowViewModel.dt_ParameterSet = MainWindow.SQLiteHelpers.ExecuteDataSet("select * from Parameters order by ParameterID asc", null).Tables[0]; //读取计划表写入缓存
//SQLiteHelpers.Close();//关闭数据库
Dictionary<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
datagrid_v.Add("Value", newValue);
//SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
//SQLiteHelpers.Open(); //打开数据库
MainWindow.SQLiteHelpers.Update("Parameters", datagrid_v, "ParameterID ='" + ID + "'", null);//更新
MainWindowViewModel.dt_ParameterSet = MainWindow.SQLiteHelpers.ExecuteDataSet("select * from Parameters order by ParameterID asc", null).Tables[0]; //读取计划表写入缓存
//SQLiteHelpers.Close();//关闭数据库
}
MainWindowViewModel.set_ = true;
}
@ -96,5 +94,74 @@ namespace DyeingComputer.View
}
return child;
}
private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int rownum = Grid.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
{
ID = (Grid.Columns[1].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第0列,
}
}
private void Grid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
currentTextBox = e.EditingElement as TextBox;
if (Keyboard == null)
{
Keyboard = new TouchKeyboard();
Keyboard.KeyPressed += Keyboard_KeyPressed;
Keyboard.Closed += (s, args) => { Keyboard = null; };
}
// 显示键盘
Keyboard.Show();
// 定位键盘位置(在主窗口下方)
UpdateKeyboardPosition();
}
// 更新键盘位置
private void UpdateKeyboardPosition()
{
if (Keyboard == null) return;
// 获取主窗口的位置
var mainWindowWidth = this.Width;
var mainWindowHeight = this.Height;
// 计算键盘位置(主窗口底部中央)
Keyboard.Left = (mainWindowWidth - Keyboard.Width) / 2;
Keyboard.Top = mainWindowHeight - (Keyboard.Height+50);
}
// 键盘按键事件处理
private void Keyboard_KeyPressed(object sender, TouchKeyboard.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;
}
Keyboard.Close();
break;
case KeyType.Hide:
// 隐藏键盘处理
Keyboard.Close();
break;
}
Grid.CommitEdit();
}
}
}

3
View/SysSetView.xaml

@ -13,7 +13,8 @@
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" >
CellEditEnding="Grid_CellEditEnding" SelectionChanged="Grid_SelectionChanged" PreparingCellForEdit="Grid_PreparingCellForEdit"
Background="White" SelectionMode="Single" FontSize="15" Cursor="AppStarting" >
<DataGrid.RowStyle >
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="30" />

93
View/SysSetView.xaml.cs

@ -1,4 +1,5 @@
using DyeingComputer.ViewModel;
using DyeingComputer.KEY;
using DyeingComputer.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
@ -24,6 +25,9 @@ namespace DyeingComputer.View
/// </summary>
public partial class SysSetView : UserControl
{
private TextBox currentTextBox;
string ID;
private TouchKeyboard Keyboard;
public SysSetView()
{
InitializeComponent();
@ -45,22 +49,15 @@ namespace DyeingComputer.View
private void Grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)//数据表输入事件
{
string ID;
string newValue = (e.EditingElement as TextBox).Text;//获得输入单元格信息
int rownum = Grid.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
{
ID = (Grid.Columns[1].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第0列,
Dictionary<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
datagrid_v.Add("Value", newValue);
//SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
//SQLiteHelpers.Open(); //打开数据库
MainWindow.SQLiteHelpers.Update("System", datagrid_v, "ParameterID ='" + ID + "'", null);//更新
MainWindowViewModel.dt_SysSet = MainWindow.SQLiteHelpers.ExecuteDataSet("select * from System order by ParameterID asc", null).Tables[0];
//SQLiteHelpers.Close();//关闭数据库
}
Dictionary<string, object> datagrid_v = new Dictionary<string, object>();//缓存函数
datagrid_v.Add("Value", newValue);
//SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
//SQLiteHelpers.Open(); //打开数据库
MainWindow.SQLiteHelpers.Update("System", datagrid_v, "ParameterID ='" + ID + "'", null);//更新
MainWindowViewModel.dt_SysSet = MainWindow.SQLiteHelpers.ExecuteDataSet("select * from System order by ParameterID asc", null).Tables[0];
//SQLiteHelpers.Close();//关闭数据库
MainWindowViewModel.set_ = true;
}
@ -92,5 +89,71 @@ namespace DyeingComputer.View
}
return child;
}
private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int rownum = Grid.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
{
ID = (Grid.Columns[1].GetCellContent(Grid.Items[rownum]) as TextBlock).Text;//定位第0列,
}
}
private void Grid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
currentTextBox = e.EditingElement as TextBox;
if (Keyboard == null)
{
Keyboard = new TouchKeyboard();
Keyboard.KeyPressed += Keyboard_KeyPressed;
Keyboard.Closed += (s, args) => { Keyboard = null; };
}
// 显示键盘
Keyboard.Show();
// 定位键盘位置(在主窗口下方)
UpdateKeyboardPosition();
}
private void UpdateKeyboardPosition()
{
if (Keyboard == null) return;
// 获取主窗口的位置
var mainWindowWidth = this.Width;
var mainWindowHeight = this.Height;
// 计算键盘位置(主窗口底部中央)
Keyboard.Left = (mainWindowWidth - Keyboard.Width) / 2;
Keyboard.Top = mainWindowHeight - (Keyboard.Height + 50);
}
// 键盘按键事件处理
private void Keyboard_KeyPressed(object sender, TouchKeyboard.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;
}
Keyboard.Close();
break;
case KeyType.Hide:
// 隐藏键盘处理
Keyboard.Close();
break;
}
Grid.CommitEdit();
}
}
}

Loading…
Cancel
Save