| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 | using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;namespace OpenSEMI.Ctrlib.Controls{	public class EditTextBlockAdorner : Adorner	{		private readonly VisualCollection _collection;		private readonly TextBoxEx _textBox;		private readonly TextBlock _textBlock;		protected override int VisualChildrenCount => _collection.Count;		public TextBoxEx TextBox => _textBox;		public event RoutedEventHandler TextBoxLostFocus		{			add			{				_textBox.LostFocus += value;			}			remove			{				_textBox.LostFocus -= value;			}		}		public event KeyEventHandler TextBoxKeyUp		{			add			{				_textBox.KeyUp += value;			}			remove			{				_textBox.KeyUp -= value;			}		}		public EditTextBlockAdorner(EditTextBlock adornedElement)			: base(adornedElement)		{			_collection = new VisualCollection(this);			_textBox = new TextBoxEx();			_textBlock = adornedElement;			Binding binding = new Binding("Text")			{				Source = adornedElement			};			binding.Mode = BindingMode.TwoWay;			_textBox.SetBinding(System.Windows.Controls.TextBox.TextProperty, binding);			_textBox.EditBoxMode = adornedElement.EditBoxMode;			_textBox.AllowEmpty = adornedElement.AllowEmpty;			_textBox.AllowBackgroundChange = adornedElement.AllowBackgroundChange;			_textBox.MaxValue = adornedElement.MaxValue;			_textBox.MinValue = adornedElement.MinValue;			_textBox.Accuracy = adornedElement.Accuracy;			_textBox.IsScrollToEnd = adornedElement.IsScrollToEnd;			binding = new Binding("TextSaved")			{				Source = adornedElement			};			binding.Mode = BindingMode.TwoWay;			_textBox.SetBinding(TextBoxEx.TextSavedProperty, binding);			_textBox.KeyUp += _textBox_KeyUp;			_collection.Add(_textBox);		}		protected override Visual GetVisualChild(int index)		{			return _collection[index];		}		protected override Size ArrangeOverride(Size finalSize)		{			_textBox.Arrange(new Rect(0.0, 0.0, _textBlock.ActualWidth, _textBlock.ActualHeight));			_textBox.Focus();			_textBox.ScrollToEnd();			return finalSize;		}		private void _textBox_KeyUp(object sender, KeyEventArgs e)		{			if (e.Key == Key.Return)			{				_textBox.Text = _textBox.Text.Replace("\r\n", string.Empty);				_textBox.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty)?.UpdateSource();				_textBox.GetBindingExpression(TextBoxEx.TextSavedProperty)?.UpdateSource();			}		}	}}
 |