嵌套滚动区 [英] Nested Scroll Areas

查看:69
本文介绍了嵌套滚动区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为WPF创建了一个控件,我想问一个关于WPF专家的问题.

I creating a control for WPF, and I have a question for you WPF gurus out there.

我希望控件能够扩展以适合可调整大小的窗口.

I want my control to be able to expand to fit a resizable window.

在我的控件中,我有一个想要随窗口扩展的列表框.列表框周围还有其他控件(按钮,文本等).

In my control, I have a list box that I want to expand with the window. I also have other controls around the list box (buttons, text, etc).

我希望能够在控件上设置一个最小尺寸,但是我希望通过创建用于查看控件的滚动条来使窗口的尺寸更小.

I want to be able to set a minimum size on my control, but I want the window to be able to be sized smaller by creating scroll bars for viewing the control.

这将创建嵌套的滚动区域:一个用于列表框,一个用于包装整个控件的ScrollViewer.

This creates nested scroll areas: One for the list box and a ScrollViewer wrapping the whole control.

现在,如果列表框设置为自动大小,则它将永远不会有滚动条,因为它始终在ScrollViewer中以全尺寸绘制.

Now, if the list box is set to auto size, it will never have a scroll bar because it is always drawn full size within the ScrollViewer.

我只希望在内容无法缩小的情况下滚动控件,否则,我不想滚动控件.相反,我想滚动控件内的列表框.

I only want the control to scroll if the content can't get any smaller, otherwise I don't want to scroll the control; instead I want to scroll the list box inside the control.

如何更改ScrollViewer类的默认行为?我尝试从ScrollViewer类继承并覆盖MeasureOverride和ArrangeOverride类,但是我不知道如何正确地测量和布置子级.看来安排必须以某种方式影响ScrollContentPresenter,而不是实际的内容子级.

How can I alter the default behavior of the ScrollViewer class? I tried inheriting from the ScrollViewer class and overriding the MeasureOverride and ArrangeOverride classes, but I couldn't figure out how to measure and arrange the child properly. It appears that the arrange has to affect the ScrollContentPresenter somehow, not the actual content child.

任何帮助/建议都将不胜感激.

Any help/suggestions would be much appreciated.

推荐答案

我创建了一个可解决此问题的类:

I've created a class to work around this problem:

public class RestrictDesiredSize : Decorator
{
    Size lastArrangeSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

    protected override Size MeasureOverride(Size constraint)
    {
        Debug.WriteLine("Measure: " + constraint);
        base.MeasureOverride(new Size(Math.Min(lastArrangeSize.Width, constraint.Width),
                                      Math.Min(lastArrangeSize.Height, constraint.Height)));
        return new Size(0, 0);
    }

    protected override Size ArrangeOverride(Size arrangeSize)
    {
        Debug.WriteLine("Arrange: " + arrangeSize);
        if (lastArrangeSize != arrangeSize) {
            lastArrangeSize = arrangeSize;
            base.MeasureOverride(arrangeSize);
        }
        return base.ArrangeOverride(arrangeSize);
    }
}

即使包含元素想要更大,它将始终返回期望的大小(0,0). 用法:

It will always return a desired size of (0,0), even if the containing element wants to be bigger. Usage:

<local:RestrictDesiredSize MinWidth="200" MinHeight="200">
     <ListBox />
</local>

这篇关于嵌套滚动区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆