private void CreateScrollbars() { if (Parent != null && !(Parent is AdornerDecorator) && (Parent is Grid || Parent is SimpleView || Parent is ContentControl)) { // We need to make sure this control is directly within an adorner decorator // so elements in the adorner layer (scroll bars) behave correctly in the Z-Order var decorator = new AdornerDecorator { VerticalAlignment = VerticalAlignment, HorizontalAlignment = HorizontalAlignment, Margin = new Thickness(Margin.Left, Margin.Top, Margin.Right, Margin.Bottom), Width = Width, Height = Height }; Grid.SetColumn(decorator, Grid.GetColumn(this)); Grid.SetRow(decorator, Grid.GetRow(this)); Grid.SetColumnSpan(decorator, Grid.GetColumnSpan(this)); Grid.SetRowSpan(decorator, Grid.GetRowSpan(this)); SimpleView.SetUIElementTitle(decorator, SimpleView.GetUIElementTitle(this)); SimpleView.SetUIElementType(decorator, SimpleView.GetUIElementType(this)); Margin = new Thickness(0d); VerticalAlignment = VerticalAlignment.Stretch; HorizontalAlignment = HorizontalAlignment.Stretch; var parentPanel = Parent as Panel; if (parentPanel != null) { var childItemIndex = -1; var found = false; foreach (var child in parentPanel.Children) { childItemIndex++; if (child == this) { found = true; break; } } if (found) { parentPanel.Children.Remove(this); parentPanel.Children.Insert(childItemIndex, decorator); decorator.Child = this; } } else { var parentContent = Parent as ContentControl; if (parentContent != null) { parentContent.Content = null; parentContent.Content = decorator; decorator.Child = this; } } } if (_scrollbarsCreated) { return; } _adorner = AdornerLayer.GetAdornerLayer(this); if (_adorner == null) { return; } _adorner.Add(new BidirectionalStackPanelScrollAdorner(this, _scrollHorizontal, _scrollVertical) { Visibility = Visibility.Visible }); _scrollHorizontal.ValueChanged += (s, e) => Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(DispatchInvalidateScroll)); _scrollVertical.ValueChanged += (s, e) => Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(DispatchInvalidateScroll)); _scrollbarsCreated = true; }