|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
-
- namespace ComeCapture.Controls
- {
- [TemplatePart(Name = "PART_TextBox", Type = typeof(TextBox))]
- public class TextBoxControl : Control
- {
- public TextBox _TextBox;
-
- static TextBoxControl()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxControl), new FrameworkPropertyMetadata(typeof(TextBoxControl)));
- }
-
- public TextBoxControl()
- {
- AddHandler(GotFocusEvent, new RoutedEventHandler((sender, e) =>
- {
- MyFocus = true;
- }));
- AddHandler(LostFocusEvent, new RoutedEventHandler((sender, e) =>
- {
- MyFocus = false;
- }));
- }
-
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- _TextBox = GetTemplateChild("PART_TextBox") as TextBox;
- _TextBox.MaxWidth = JieTuWindow.Current.MainImage.Width - JieTuWindow.Current.MainImage.point.X - 3;
- _TextBox.MaxHeight = JieTuWindow.Current.MainImage.Height - JieTuWindow.Current.MainImage.point.Y - 3;
- }
-
- #region BorderColor DependencyProperty
-
- public Color BorderColor
- {
- get => (Color)GetValue(BorderColorProperty);
- set => SetValue(BorderColorProperty, value);
- }
-
- public static readonly DependencyProperty BorderColorProperty =
- DependencyProperty.Register("BorderColor", typeof(Color), typeof(TextBoxControl),
- new PropertyMetadata(Colors.Transparent, new PropertyChangedCallback(TextBoxControl.OnBorderColorPropertyChanged)));
-
- private static void OnBorderColorPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
- {
- if (obj is TextBoxControl)
- {
- (obj as TextBoxControl).OnBorderColorValueChanged();
- }
- }
-
- protected void OnBorderColorValueChanged()
- {
- }
-
- #endregion BorderColor DependencyProperty
-
- #region MyFocus DependencyProperty
-
- public bool MyFocus
- {
- get => (bool)GetValue(MyFocusProperty);
- set => SetValue(MyFocusProperty, value);
- }
-
- public static readonly DependencyProperty MyFocusProperty =
- DependencyProperty.Register("MyFocus", typeof(bool), typeof(TextBoxControl),
- new PropertyMetadata(true, new PropertyChangedCallback(TextBoxControl.OnMyFocusPropertyChanged)));
-
- private static void OnMyFocusPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
- {
- if (obj is TextBoxControl)
- {
- (obj as TextBoxControl).OnMyFocusValueChanged();
- }
- }
-
- protected void OnMyFocusValueChanged()
- {
- if (!MyFocus)
- {
- if (string.IsNullOrEmpty(_TextBox.Text))
- {
- JieTuWindow.RemoveControl(this);
- }
- else
- {
- JieTuWindow.Current.MainImage.ResetLimit(Canvas.GetLeft(this), Canvas.GetTop(this), (Canvas.GetLeft(this) + ActualWidth), (Canvas.GetTop(this) + ActualHeight));
- JieTuWindow.Register(this);
- }
- JieTuWindow.Current.MainImage._Text = null;
- }
- }
-
- #endregion MyFocus DependencyProperty
- }
- }
|