| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using SKMC.UI.UserControls;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace SKMC.UI.CustomControls
- {
- public class SKTextBox : TextBox
- {
- static SKTextBox()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(SKTextBox), new FrameworkPropertyMetadata(typeof(SKTextBox)));
- }
- public SKTextBox()
- {
- this.PreviewMouseDown += SKTextBox_PreviewMouseDown;
- //this.GotFocus += SKTextBox_GotFocus;
- }
- public string Label
- {
- get { return (string)GetValue(LabelProperty); }
- set { SetValue(LabelProperty, value); }
- }
- /// <summary>
- /// 单位
- /// </summary>
- public string Unit
- {
- get { return (string)GetValue(UnitProperty); }
- set { SetValue(UnitProperty, value); }
- }
- /// <summary>
- /// 单位字体大小
- /// </summary>
- public double UnitSize
- {
- get { return (double)GetValue(UnitSizeProperty); }
- set { SetValue(UnitSizeProperty, value); }
- }
- /// <summary>
- /// 值是否有效
- /// </summary>
- public bool IsValueValid
- {
- get { return (bool)GetValue(IsValueValidProperty); }
- set { SetValue(IsValueValidProperty, value); }
- }
- /// <summary>
- /// 最大值
- /// </summary>
- public object MaxValue
- {
- get { return (object)GetValue(MaxValueProperty); }
- set { SetValue(MaxValueProperty, value); }
- }
- /// <summary>
- /// 最小值
- /// </summary>
- public object MinValue
- {
- get { return (object)GetValue(MinValueProperty); }
- set { SetValue(MinValueProperty, value); }
- }
- /// <summary>
- /// 圆角
- /// </summary>
- public CornerRadius CornerRadius
- {
- get { return (CornerRadius)GetValue(CornerRadiusProperty); }
- set { SetValue(CornerRadiusProperty, value); }
- }
- public string ToolTipComment
- {
- get { return (string)GetValue(ToolTipCommentProperty); }
- set { SetValue(ToolTipCommentProperty, value); }
- }
- /// <summary>
- /// 图标
- /// </summary>
- public Geometry Icon
- {
- get { return (Geometry)GetValue(IconProperty); }
- set { SetValue(IconProperty, value); }
- }
- public double IconSize
- {
- get { return (double)GetValue(IconSizeProperty); }
- set { SetValue(IconSizeProperty, value); }
- }
- /// <summary>
- /// 数字键盘激活
- /// </summary>
- public bool NumpadActivity
- {
- get { return (bool)GetValue(NumpadActivityProperty); }
- set { SetValue(NumpadActivityProperty, value); }
- }
- public static readonly DependencyProperty NumpadActivityProperty =
- DependencyProperty.Register(nameof(NumpadActivity), typeof(bool), typeof(SKTextBox), new PropertyMetadata(false));
- public static readonly DependencyProperty IconProperty =
- DependencyProperty.Register("Icon", typeof(Geometry), typeof(SKTextBox), new PropertyMetadata(null));
- public static readonly DependencyProperty ToolTipCommentProperty =
- DependencyProperty.Register("ToolTipComment", typeof(string), typeof(SKTextBox), new PropertyMetadata(null));
- public static readonly DependencyProperty CornerRadiusProperty =
- DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(SKTextBox), new PropertyMetadata(new CornerRadius(0)));
- public static readonly DependencyProperty MinValueProperty =
- DependencyProperty.Register("MinValue", typeof(object), typeof(SKTextBox), new PropertyMetadata(null));
- public static readonly DependencyProperty MaxValueProperty =
- DependencyProperty.Register("MaxValue", typeof(object), typeof(SKTextBox), new PropertyMetadata(null));
- public static readonly DependencyProperty IsValueValidProperty =
- DependencyProperty.Register("IsValueValid", typeof(bool), typeof(SKTextBox), new PropertyMetadata(true));
- public static readonly DependencyProperty UnitProperty =
- DependencyProperty.Register("Unit", typeof(string), typeof(SKTextBox), new PropertyMetadata(""));
- public static readonly DependencyProperty LabelProperty =
- DependencyProperty.Register("Label", typeof(string), typeof(SKTextBox), new PropertyMetadata(""));
- public static readonly DependencyProperty UnitSizeProperty =
- DependencyProperty.Register("UnitSize", typeof(double), typeof(SKTextBox), new PropertyMetadata(10.0));
- public static readonly DependencyProperty IconSizeProperty =
- DependencyProperty.Register(nameof(IconSize), typeof(double), typeof(SKTextBox), new PropertyMetadata(10.0));
- /// <summary>
- /// 鼠标点击时激活键盘
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void SKTextBox_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
- {
- if (!NumpadActivity) return;
- if (sender is SKTextBox textBox)
- {
- // 打开数字键盘
- OpenNumpad(textBox);
- // 防止默认的焦点行为
- e.Handled = true;
- }
- }
- // 获取焦点时激活键盘(作为备选)
- //private void SKTextBox_GotFocus(object sender, RoutedEventArgs e)
- //{
- // if (sender is SKTextBox textBox)
- // {
- // OpenNumpad(textBox);
- // }
- //}
- // 打开数字键盘
- private void OpenNumpad(SKTextBox textBox)
- {
- if (!NumpadActivity) return;
- try
- {
- // 创建键盘窗口
- NumpadControl numpadWindow = new NumpadControl(textBox);
- // 设置窗口位置在父窗口中央
- numpadWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
- // 显示对话框
- bool? result = numpadWindow.ShowDialog();
- // 如果确认了输入,更新文本框
- if (result == true && numpadWindow.IsConfirmed)
- {
- textBox.Text = numpadWindow.InputResult;
- // 更新光标位置
- textBox.CaretIndex = textBox.Text.Length;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"打开键盘失败:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
- }
|