用于可调整大小的窗口的 WPF 滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2337477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:31:30  来源:igfitidea点击:

WPF scrollbar for a resizable window

wpfwpf-controlsscrollscrollviewer

提问by Greg R

This should be a very simple task, but for some reason I'm running into a lot of problems with it in WPF.

这应该是一项非常简单的任务,但出于某种原因,我在 WPF 中遇到了很多问题。

This is what I want to happen: I have a bunch of controls in a window, including expander controls. I want to have scroll bars for that window, when content expands below the visible area. Also, the window is not of fixed width, it can be maximized, resized, etc.

这就是我想要发生的事情:我在一个窗口中有一堆控件,包括扩展器控件。当内容扩展到可见区域下方时,我想为该窗口设置滚动条。此外,窗口不是固定宽度的,可以最大化、调整大小等。

I tried putting a ScrollViewer as the first element in the window, but it's not working correctly. If I set the height and width to Auto, it doesn't scroll and if i set it to spefic detentions, it creates a box when the window is maximized.

我尝试将 ScrollViewer 作为窗口中的第一个元素,但它无法正常工作。如果我将高度和宽度设置为自动,则它不会滚动,如果我将其设置为特定滞留,它会在窗口最大化时创建一个框。

Any help would be greatly appreciated!

任何帮助将不胜感激!

回答by bendewey

I'm assuming that you have some fixed width issues. If you provide a sample of your XAML I can see if I can help further. The following works without showing a box:

我假设您有一些固定宽度问题。如果您提供 XAML 示例,我可以看看我是否可以提供进一步帮助。以下工作不显示框:

<Window x:Class="WpfSample1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <ScrollViewer>
        <StackPanel>
            <Rectangle Height="400" Width="400" Fill="Red" Margin="10" />
            <Rectangle Height="400" Width="400" Fill="Green" Margin="10" />
            <Rectangle Height="400" Width="400" Fill="Blue" Margin="10" />
            <Rectangle Height="400" Width="400" Fill="Yellow" Margin="10" />
        </StackPanel>
    </ScrollViewer>
</Window>

回答by Timores

You should set the HorizontalScrollBarVisibility and the VerticalScrollBarVisibility of the ScrollViewer to Auto.

您应该将 ScrollViewer 的 Horizo​​ntalScrollBarVisibility 和 VerticalScrollBarVisibility 设置为 Auto。

Here is an example:

下面是一个例子:

<Grid>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Canvas Width="400" Height="400">
            <Button Canvas.Left="300">Left 300</Button>
            <Button Canvas.Top="300">Top 300</Button>
        </Canvas>
    </ScrollViewer>
</Grid>

This replaces the content of the main window generated by VS.

这将替换 VS 生成的主窗口的内容。

Run it and change the size of the window, maximize it and you'll scroll bars appearing and disappearing.

运行它并更改窗口的大小,将其最大化,您将滚动条出现和消失。