后台代码如下:
文章插图
文章插图
public partial class KeyboardControl : Window{private int TextIndex { get; set; }public string TextStr { get; private set; }//通过该属性,访问Keyboard的文本public KeyboardControl(string inputStr)//构造方式传入初始文本{InitializeComponent();TextStr = inputStr;tbValue.Text = inputStr;tbValue.Focus();tbValue.CaretIndex = inputStr.Length;}public static readonly DependencyProperty CapsProperty = DependencyProperty.Register("Caps", typeof(bool), typeof(KeyboardControl), new PropertyMetadata(default(bool)));public bool Caps{get { return (bool)GetValue(CapsProperty); }set { SetValue(CapsProperty, value); }}private void Button_Click(object sender, RoutedEventArgs e){Button button = (Button)sender;if (TextIndex == 0){tbValue.Text += (string)button.Content;}else{tbValue.Text = tbValue.Text.Insert(TextIndex, (string)button.Content);}}private void tbValue_TextChanged(object sender, RoutedEventArgs e){TextBox textBox = (TextBox)sender;TextIndex = textBox.CaretIndex;}private void ClearButton_Click(object sender, RoutedEventArgs e){tbValue.Text = "";}private void DELButton_Click(object sender, RoutedEventArgs e){if (tbValue.Text.Length > 0){if (TextIndex == 0 && tbValue.Text.Length >= 1){tbValue.Text = tbValue.Text.Remove(tbValue.Text.Length - 1, 1);}else if (TextIndex > 0){tbValue.Text = tbValue.Text.Remove(TextIndex - 1, 1);}}}private void OKButton_Click(object sender, RoutedEventArgs e){TextStr = tbValue.Text;DialogResult = true;Close();}private void CancelButton_Click(object sender, RoutedEventArgs e){DialogResult = false;Close();}private void CapsButton_Click(object sender, RoutedEventArgs e){Caps = !Caps;}}Xaml代码中用到了一个大小写的转换类:
文章插图
文章插图
public class CapsConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (parameter == null){return "";}if (value =https://www.huyubaike.com/biancheng/= null){return parameter.ToString();}if (value is bool b){return b ? parameter.ToString().ToUpper() : parameter.ToString().ToLower();}else{return parameter.ToString();}}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}三 TouchTextBox定义一个TouchTextBox的分部类 。
文章插图
文章插图
public partial class TouchTextBox{private Control hostControl;//OnClick方法调用时 , 通过Window.ShowDialog方法,打开KeyboardControlpublic void OnClick(object sender, MouseButtonEventArgs e){if (sender is TextBox textBox){hostControl = textBox;//计算KeyboardControl的位置 , 弹出KeyboardControlvar text = Show(textBox.Text, textBox);//KeyboardControl关闭后,获取其文本值,赋值给TextBoxif (!string.IsNullOrEmpty(text)){textBox.Text = text;}else{textBox.Text = string.Empty;}}}private string Show(string initValue, object sender = null){var keyboard = new KeyboardControl(initValue);SetPosition(keyboard);bool result = keyboard.ShowDialog().Value;if (result){return keyboard.TextStr;}else{return string.Empty;}}private void SetPosition(Window window){Point point = hostControl.PointFromScreen(new Point(0.0, 0.0));double width = SystemParameters.WorkArea.Width;double height = SystemParameters.WorkArea.Height;if (-point.Y + hostControl.ActualHeight + 5.0 + window.Height < height){window.Top = -point.Y + hostControl.ActualHeight + 5.0;}else{window.Top = -point.Y - window.Height - 5.0;}if (-point.X + window.Width < width){window.Left = -point.X;}else{window.Left = -point.X - (window.Width - hostControl.ActualWidth);}}}添加一个名为TouchTextBox的资源字典 。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"x:Class="WpfApp1.TouchTextBox"><Style x:Key="TouchTextBox" TargetType="{x:Type TextBox}"><EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnClick" /></Style></ResourceDictionary>四 效果展示在App.Xaml中引入TouchTextBox.Xaml资源 。
<Application x:Class="WpfApp1.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp1"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/WpfApp1;component/TouchTextBox.xaml" /></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources></Application>
推荐阅读
- 驱动开发:通过ReadFile与内核层通信
- 三十六 Java开发学习----SpringBoot三种配置文件解析
- 在 WPF 中实现融合效果
- aardio + Python 可视化快速开发桌面程序,一键生成独立 EXE
- Windows esp-idf 安装
- aardio + PHP 可视化快速开发独立 EXE 桌面程序
- 制造企业有可能自行开发ERP系统吗?
- aardio + VBScript 混合开发
- 14 基于SqlSugar的开发框架循序渐进介绍-- 基于Vue3+TypeScript的全局对象的注入和使用
- 基于Qt Designer和PyQt5的桌面软件开发--环境搭建和入门例子