嵌套滚动区域 [英] Nested Scroll Areas

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

问题描述

我为 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天全站免登陆