123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace OpenSEMI.Ctrlib.Controls
- {
- public class EditTextBlock : TextBlock
- {
- public static readonly DependencyProperty IsInEditModeProperty;
- public static readonly DependencyProperty EditBoxModeProperty;
- private bool m_AllowEmpty = true;
- private bool m_AllowBackgroundChange = false;
- public static readonly DependencyProperty MaxValueProperty;
- public static readonly DependencyProperty MinValueProperty;
- public static readonly DependencyProperty AccuracyProperty;
- public static readonly DependencyProperty TextSavedProperty;
- public static readonly DependencyProperty ScrollToEndProperty;
- private EditTextBlockAdorner _adorner;
- public bool IsInEditMode
- {
- get
- {
- return (bool)GetValue(IsInEditModeProperty);
- }
- set
- {
- SetValue(IsInEditModeProperty, value);
- }
- }
- public EditBoxMode EditBoxMode
- {
- get
- {
- return (EditBoxMode)GetValue(EditBoxModeProperty);
- }
- set
- {
- SetValue(EditBoxModeProperty, value);
- }
- }
- public bool AllowEmpty
- {
- get
- {
- return m_AllowEmpty;
- }
- set
- {
- m_AllowEmpty = value;
- }
- }
- public bool AllowBackgroundChange
- {
- get
- {
- return m_AllowBackgroundChange;
- }
- set
- {
- m_AllowBackgroundChange = value;
- }
- }
- public double MaxValue
- {
- get
- {
- return (double)GetValue(MaxValueProperty);
- }
- set
- {
- SetValue(MaxValueProperty, value);
- }
- }
- public double MinValue
- {
- get
- {
- return (double)GetValue(MinValueProperty);
- }
- set
- {
- SetValue(MinValueProperty, value);
- }
- }
- public int Accuracy
- {
- get
- {
- return (int)GetValue(AccuracyProperty);
- }
- set
- {
- SetValue(AccuracyProperty, value);
- }
- }
- public bool TextSaved
- {
- get
- {
- return (bool)GetValue(TextSavedProperty);
- }
- set
- {
- SetValue(TextSavedProperty, value);
- }
- }
- public bool IsScrollToEnd
- {
- get
- {
- return (bool)GetValue(ScrollToEndProperty);
- }
- set
- {
- SetValue(ScrollToEndProperty, value);
- }
- }
- static EditTextBlock()
- {
- IsInEditModeProperty = DependencyProperty.Register("IsInEditMode", typeof(bool), typeof(EditTextBlock), new UIPropertyMetadata(false, IsInEditModeUpdate));
- EditBoxModeProperty = DependencyProperty.Register("EditBoxMode", typeof(EditBoxMode), typeof(EditTextBlock), new UIPropertyMetadata(EditBoxMode.Default));
- MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(EditTextBlock), new UIPropertyMetadata(double.NaN));
- MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(EditTextBlock), new UIPropertyMetadata(double.NaN));
- AccuracyProperty = DependencyProperty.Register("Accuracy", typeof(int), typeof(EditTextBlock), new UIPropertyMetadata(4));
- TextSavedProperty = DependencyProperty.Register("TextSaved", typeof(bool), typeof(EditTextBlock), new UIPropertyMetadata(true, TextSavedChangedCallBack));
- ScrollToEndProperty = DependencyProperty.Register("IsScrollToEnd", typeof(bool), typeof(EditTextBlock), new UIPropertyMetadata(false));
- FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(EditTextBlock), new FrameworkPropertyMetadata(typeof(EditTextBlock)));
- }
- private static void IsInEditModeUpdate(DependencyObject obj, DependencyPropertyChangedEventArgs e)
- {
- EditTextBlock editTextBlock = obj as EditTextBlock;
- if (editTextBlock != null)
- {
- AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(editTextBlock);
- if (adornerLayer != null)
- {
- if (editTextBlock.IsInEditMode)
- {
- if (editTextBlock._adorner == null)
- {
- editTextBlock._adorner = new EditTextBlockAdorner(editTextBlock);
- editTextBlock._adorner.TextBoxKeyUp += editTextBlock.TextBoxKeyUp;
- editTextBlock._adorner.TextBoxLostFocus += editTextBlock.TextBoxLostFocus;
- }
- adornerLayer.Add(editTextBlock._adorner);
- }
- else
- {
- Adorner[] adorners = adornerLayer.GetAdorners(editTextBlock);
- if (adorners != null)
- {
- Adorner[] array = adorners;
- foreach (Adorner adorner in array)
- {
- if (adorner is EditTextBlockAdorner)
- {
- adornerLayer.Remove(adorner);
- }
- }
- }
- }
- }
- }
- }
- private void TextBoxLostFocus(object sender, RoutedEventArgs e)
- {
- IsInEditMode = false;
- TextBoxEx textBoxEx = sender as TextBoxEx;
- if (textBoxEx != null && (EditBoxMode == EditBoxMode.Decimal || EditBoxMode == EditBoxMode.UnSignDecimal || EditBoxMode == EditBoxMode.SignInteger || EditBoxMode == EditBoxMode.UnSignInteger))
- {
- double result = 0.0;
- if (double.TryParse(textBoxEx.Text, out result) && (MinValue != MaxValue || MinValue != 0.0))
- {
- base.Background = ((result < MinValue || result > MaxValue) ? Brushes.Red : Brushes.Transparent);
- }
- }
- }
- private void TextBoxKeyUp(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Return)
- {
- IsInEditMode = false;
- TextBoxEx textBoxEx = sender as TextBoxEx;
- if (textBoxEx != null && (EditBoxMode == EditBoxMode.Decimal || EditBoxMode == EditBoxMode.UnSignDecimal || EditBoxMode == EditBoxMode.SignInteger || EditBoxMode == EditBoxMode.UnSignInteger))
- {
- double result = 0.0;
- if (double.TryParse(textBoxEx.Text, out result) && (MinValue != MaxValue || MinValue != 0.0))
- {
- base.Background = ((result < MinValue || result > MaxValue) ? Brushes.Red : Brushes.Transparent);
- }
- }
- }
- }
- protected override void OnMouseDown(MouseButtonEventArgs e)
- {
- if (e.MiddleButton == MouseButtonState.Pressed)
- {
- IsInEditMode = true;
- }
- else if (e.ClickCount == 1)
- {
- IsInEditMode = true;
- }
- }
- private static void TextSavedChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
- {
- EditTextBlock editTextBlock = d as EditTextBlock;
- if (editTextBlock == null)
- {
- return;
- }
- }
- }
- }
|