From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点
---
HMI.UI/EditBox.cs | 194 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 194 insertions(+), 0 deletions(-)
diff --git a/HMI.UI/EditBox.cs b/HMI.UI/EditBox.cs
new file mode 100644
index 0000000..9d7b8bd
--- /dev/null
+++ b/HMI.UI/EditBox.cs
@@ -0,0 +1,194 @@
+锘縰sing System;
+using System.ComponentModel;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+using System.Windows.Media;
+
+namespace HMI.UI
+{
+ public class EditBox : Control
+ {
+ static EditBox()
+ {
+ DefaultStyleKeyProperty.OverrideMetadata(typeof(EditBox), new FrameworkPropertyMetadata(typeof(EditBox)));
+ }
+
+ public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
+ "Value",
+ typeof(object),
+ typeof(EditBox),
+ new FrameworkPropertyMetadata());
+
+ public static readonly DependencyProperty CanEditProperty = DependencyProperty.Register(
+ "CanEdit",
+ typeof(bool),
+ typeof(EditBox),
+ new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnCanEditPropertyChanged)));
+
+ public static DependencyProperty IsEditingProperty = DependencyProperty.Register(
+ "IsEditing",
+ typeof(bool),
+ typeof(EditBox),
+ new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsEditingPropertyChanged)));
+
+ public static DependencyProperty PreEditingProperty = DependencyProperty.Register(
+ "PreEditing",
+ typeof(Func<bool?>),
+ typeof(EditBox),
+ new FrameworkPropertyMetadata());
+
+ public static DependencyProperty ShowEmptyProperty = DependencyProperty.Register(
+ "ShowEmpty",
+ typeof(bool),
+ typeof(EditBox),
+ new FrameworkPropertyMetadata(false));
+
+ private static void OnCanEditPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
+ {
+ if (obj is EditBox eb && e.Property == CanEditProperty)
+ {
+ bool value = (bool)e.NewValue;
+ if (value)
+ {
+ eb.MouseDoubleClick += eb.EditBox_MouseDoubleClick;
+ }
+ else
+ {
+ eb.MouseDoubleClick -= eb.EditBox_MouseDoubleClick;
+ }
+ }
+ }
+
+ private static void OnIsEditingPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
+ {
+ if (obj is EditBox eb && e.Property == IsEditingProperty)
+ {
+ bool value = (bool)e.NewValue;
+ eb.editing.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
+ if (value)
+ {
+ eb.editing.Focus();
+ }
+ }
+ }
+
+ private TextBox editing;
+ public EditBox()
+ {
+ this.Loaded += EditBox_Loaded;
+ }
+
+ private void EditBox_Loaded(object sender, RoutedEventArgs e)
+ {
+ editing = this.Template.FindName("PART_TextBoxPart", this) as TextBox;
+ }
+
+ private void EditBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
+ {
+ if (IsEditing)
+ {
+ return;
+ }
+
+ Func<bool?> pre = GetValue(PreEditingProperty) as Func<bool?>;
+ if (pre != null && pre.Invoke() != true)
+ {
+ return;
+ }
+ var win = Window.GetWindow(this);
+
+ win.MouseDown += EditBox_LostFocus;
+ editing.LostFocus += EditBox_LostFocus;
+ IsEditing = true;
+ e.Handled = true;
+ }
+
+ public static bool GetElementUnderMouse(UIElement relativeTo, UIElement target)
+ {
+ Point mousePosition = Mouse.GetPosition(relativeTo);
+ bool res = false;
+ HitTestResultCallback resultCallback = (result) =>
+ {
+ // 鑾峰彇鐩镐氦鐨刄I鍏冪礌
+ var element = result.VisualHit as FrameworkElement;
+
+ if (element != null && element == target)
+ {
+ res = true;
+ return HitTestResultBehavior.Stop; // 鎵惧埌鍚庡仠姝㈤亶鍘�
+ }
+ return HitTestResultBehavior.Continue;
+ };
+
+ VisualTreeHelper.HitTest(relativeTo, null,
+ resultCallback, new PointHitTestParameters(mousePosition));
+ return res;
+ }
+
+ private void EditBox_LostFocus(object sender, RoutedEventArgs e)
+ {
+ if (sender == editing)
+ {
+ IsEditing = false;
+ editing.LostFocus -= EditBox_LostFocus;
+ Window.GetWindow(this).MouseDown -= EditBox_LostFocus;
+ }
+ else
+ {
+ var win = sender as Window;
+ if (!GetElementUnderMouse(win, this))
+ {
+ //editing.Focu
+ IsEditing = false;
+ win.MouseDown -= EditBox_LostFocus;
+ Window.GetWindow(this).MouseDown -= EditBox_LostFocus;
+ }
+ }
+ }
+
+ [Bindable(true)]
+ [Category("Appearance")]
+ public object Value
+ {
+ get { return GetValue(ValueProperty); }
+ set { SetValue(ValueProperty, value); }
+ }
+
+ public bool CanEdit
+ {
+ get => (bool)GetValue(CanEditProperty);
+ set
+ {
+ SetValue(CanEditProperty, value);
+ }
+ }
+
+ public bool IsEditing
+ {
+ get => (bool)GetValue(IsEditingProperty);
+ set
+ {
+ SetValue(IsEditingProperty, value);
+ }
+ }
+
+ public Func<bool?> PreEditing
+ {
+ get => (Func<bool?>)GetValue(PreEditingProperty);
+ set
+ {
+ SetValue(PreEditingProperty, value);
+ }
+ }
+
+ public bool ShowEmpty
+ {
+ get => (bool)GetValue(ShowEmptyProperty);
+ set
+ {
+ SetValue(ShowEmptyProperty, value);
+ }
+ }
+ }
+}
--
Gitblit v1.9.1