/// <summary> /// Walk the visual tree to find the ItemsControl and /// hook into some of its events. This is done to make /// sure that editing is cancelled whenever /// /// 1> The parent control is scrolling its content /// 1.1> The MouseWheel is used /// 2> A user clicks outside the adorner control /// 3> The parent control changes its size /// /// </summary> private void HookItemsControlEvents() { if (_PART_MeasureTextBlock == null) { return; } _TextBox = new TextBox(); _Adorner = new EditBoxAdorner(_PART_MeasureTextBlock, _TextBox, this); AdornerLayer layer = AdornerLayer.GetAdornerLayer(_PART_MeasureTextBlock); if (layer == null) // This layer does not always seem to be available? { return; } layer.Add(_Adorner); // try to get the text box focused when layout finishes. _TextBox.LayoutUpdated += new EventHandler(this.OnTextBoxLayoutUpdated); CaptureMouse(); Mouse.AddPreviewMouseDownOutsideCapturedElementHandler(this, OnMouseDownOutsideElement); _TextBox.PreviewTextInput += OnPreviewTextInput; _TextBox.KeyDown += new KeyEventHandler(this.OnTextBoxKeyDown); _TextBox.LostKeyboardFocus += new KeyboardFocusChangedEventHandler(this.OnTextBoxLostKeyboardFocus); _TextBox.LostFocus += new RoutedEventHandler(this.OnLostFocus); _ParentItemsControl = this.GetDpObjectFromVisualTree(this, typeof(ItemsControl)) as ItemsControl; Debug.Assert(_ParentItemsControl != null, "DEBUG ISSUE: No FolderTreeView found."); if (_ParentItemsControl != null) { // Handle events on parent control and determine whether to switch to Normal mode or stay in editing mode _ParentItemsControl.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(this.OnScrollViewerChanged)); _ParentItemsControl.AddHandler(ScrollViewer.MouseWheelEvent, new RoutedEventHandler((s, e) => this.OnSwitchToNormalMode()), true); _ParentItemsControl.MouseDown += new MouseButtonEventHandler((s, e) => this.OnSwitchToNormalMode()); _ParentItemsControl.SizeChanged += new SizeChangedEventHandler((s, e) => this.OnSwitchToNormalMode()); // Restrict text box to visible area of scrollviewer this.ParentScrollViewer = this.GetDpObjectFromVisualTree(_ParentItemsControl, typeof(ScrollViewer)) as ScrollViewer; if (this.ParentScrollViewer == null) { this.ParentScrollViewer = FindVisualChild <ScrollViewer>(_ParentItemsControl); } Debug.Assert(this.ParentScrollViewer != null, "DEBUG ISSUE: No ScrollViewer found."); if (this.ParentScrollViewer != null) { _TextBox.MaxWidth = this.ParentScrollViewer.ViewportWidth; } } }
/// <summary> /// Walk the visual tree to find the ItemsControl and /// hook into some of its events. This is done to make /// sure that editing is cancelled whenever /// /// 1> The parent control is scrolling its content /// 1.1> The MouseWheel is used /// 2> A user clicks outside the adorner control /// 3> The parent control changes its size /// /// </summary> private void HookItemsControlEvents() { if (this.mPART_MeasureTextBlock == null) { return; } this.mTextBox = new TextBox(); this.mAdorner = new EditBoxAdorner(this.mPART_MeasureTextBlock, this.mTextBox, this); AdornerLayer layer = AdornerLayer.GetAdornerLayer(this.mPART_MeasureTextBlock); layer.Add(this.mAdorner); this.mTextBox.PreviewTextInput += OnPreviewTextInput; this.mTextBox.KeyDown += new KeyEventHandler(this.OnTextBoxKeyDown); this.mTextBox.LostKeyboardFocus += new KeyboardFocusChangedEventHandler(this.OnTextBoxLostKeyboardFocus); this.mTextBox.LostFocus += new RoutedEventHandler(this.OnLostFocus); this.mParentItemsControl = this.GetDpObjectFromVisualTree(this, typeof(ItemsControl)) as ItemsControl; Debug.Assert(this.mParentItemsControl != null, "DEBUG ISSUE: No FolderTreeView found."); if (this.mParentItemsControl != null) { // Handle events on parent control and determine whether to switch to Normal mode or stay in editing mode this.mParentItemsControl.AddHandler(ScrollViewer.ScrollChangedEvent, new RoutedEventHandler(this.OnScrollViewerChanged)); this.mParentItemsControl.AddHandler(ScrollViewer.MouseWheelEvent, new RoutedEventHandler((s, e) => this.OnSwitchToNormalMode()), true); this.mParentItemsControl.MouseDown += new MouseButtonEventHandler((s, e) => this.OnSwitchToNormalMode()); this.mParentItemsControl.SizeChanged += new SizeChangedEventHandler((s, e) => this.OnSwitchToNormalMode()); // Restrict text box to visible area of scrollviewer this.ParentScrollViewer = this.GetDpObjectFromVisualTree(this.mParentItemsControl, typeof(ScrollViewer)) as ScrollViewer; if (this.ParentScrollViewer == null) { this.ParentScrollViewer = FindVisualChild <ScrollViewer>(this.mParentItemsControl); } Debug.Assert(this.ParentScrollViewer != null, "DEBUG ISSUE: No ScrollViewer found."); if (this.ParentScrollViewer != null) { this.mTextBox.MaxWidth = this.ParentScrollViewer.ViewportWidth; } } }