WPF托管窗口在ScrollViewer中形成元素

时间:2008-09-23 16:53:43

标签: wpf

将ScrollViewer放入窗口时(不保留窗口的所有大小) 在ScrollViewer中有(其他东西)一个WinFormsHost和一个内部控件(让我们说一个DateTimePicker)。当滚动时,内部winforms控件在不再有原因(它在滚动区域之外)时保持可见,因此它“漂浮”在ScrollViewer之外的“浮动”

任何解决方案?

3 个答案:

答案 0 :(得分:6)

根据this msdn link

  

WindowsFormsHost元素总是如此   在其他WPF元素之上绘制,   并且它们不受z顺序的影响

我认为没有一个简单的解决方案。您可能需要考虑让窗体控件处理滚动本身而不是使用WPF的ScrollViewer。

答案 1 :(得分:3)

是的,有一个解决方案。使用以下自定义WindowsFormsHost类。

class WindowsFormsHostEx : WindowsFormsHost
    {
        private PresentationSource _presentationSource;

        public WindowsFormsHostEx()
        {
            PresentationSource.AddSourceChangedHandler(this, SourceChangedEventHandler);
        }

        protected override void OnWindowPositionChanged(Rect rcBoundingBox)
        {
            base.OnWindowPositionChanged(rcBoundingBox);

            ParentScrollViewer.ScrollChanged += ParentScrollViewer_ScrollChanged;
            ParentScrollViewer.SizeChanged += ParentScrollViewer_SizeChanged;
            ParentScrollViewer.Loaded += ParentScrollViewer_Loaded;

            if (Scrolling || Resizing)
            {
                if (ParentScrollViewer == null)
                    return;
                GeneralTransform tr = RootVisual.TransformToDescendant(ParentScrollViewer);
                var scrollRect = new Rect(new Size(ParentScrollViewer.ViewportWidth, ParentScrollViewer.ViewportHeight));

                var intersect = Rect.Intersect(scrollRect, tr.TransformBounds(rcBoundingBox));
                if (!intersect.IsEmpty)
                {
                    tr = ParentScrollViewer.TransformToDescendant(this);
                    intersect = tr.TransformBounds(intersect);
                }
                else
                    intersect = new Rect();

                int x1 = (int)Math.Round(intersect.Left);
                int y1 = (int)Math.Round(intersect.Top);
                int x2 = (int)Math.Round(intersect.Right);
                int y2 = (int)Math.Round(intersect.Bottom);

                SetRegion(x1, y1, x2, y2);
                this.Scrolling = false;
                this.Resizing = false;

            }

        }

        private void ParentScrollViewer_Loaded(object sender, RoutedEventArgs e)
        {
            this.Resizing = true;
        }

        private void ParentScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Resizing = true;
        }

        private void ParentScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.VerticalChange != 0 || e.HorizontalChange != 0 || e.ExtentHeightChange != 0 || e.ExtentWidthChange != 0)
                Scrolling = true;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
                PresentationSource.RemoveSourceChangedHandler(this, SourceChangedEventHandler);
        }

        private void SourceChangedEventHandler(Object sender, SourceChangedEventArgs e)
        {
            ParentScrollViewer = FindParentScrollViewer();
        }

        private ScrollViewer FindParentScrollViewer()
        {
            DependencyObject vParent = this;
            ScrollViewer parentScroll = null;
            while (vParent != null)
            {
                parentScroll = vParent as ScrollViewer;
                if (parentScroll != null)
                    break;

                vParent = LogicalTreeHelper.GetParent(vParent);
            }
            return parentScroll;
        }

        private void SetRegion(int x1, int y1, int x2, int y2)
        {
            SetWindowRgn(Handle, CreateRectRgn(x1, y1, x2, y2), true);
        }

        private Visual RootVisual
        {
            get
            {
                _presentationSource = PresentationSource.FromVisual(this);
                return _presentationSource.RootVisual;
            }
        }

        private ScrollViewer ParentScrollViewer { get; set; }

        private bool Scrolling { get; set; }
        private bool Resizing { get; set; }

        [DllImport("User32.dll", SetLastError = true)]
        static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

        [DllImport("gdi32.dll")]
        static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
    }

答案 2 :(得分:0)

只需添加ScrollViewer控件属性:

VerticalScrollBarVisibility="Auto"

并将高度设置为您的最大高度。就是这样。