WPF:带滚动条的 ItemsControl (ScrollViewer)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2028459/
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:24:27  来源:igfitidea点击:

WPF: ItemsControl with scrollbar (ScrollViewer)

wpfwpf-controlsitemscontrolscrollviewer

提问by Xuntar

I followed thissmall "tutorial" on how to add a scrollbar to an ItemsControl, and it works in Designer view, but not when I compile and execute the program (only the first few items show up, and no scrollbar to view more - even when VerticalScrollbarVisibility is set to "Visible" instead of "Auto").

我遵循了这个关于如何向 ItemsControl 添加滚动条的小“教程”,它在设计器视图中工作,但在我编译和执行程序时不起作用(只显示前几个项目,没有滚动条查看更多 - 甚至当 VerticalScrollbarVisibility 设置为“可见”而不是“自动”时)。

Any idea on how to solve this?

关于如何解决这个问题的任何想法?



This is the code I use to show my items (normally I work with Databinding, but to see the items in my Designer I added them manually):

这是我用来显示我的项目的代码(通常我使用数据绑定,但为了在我的设计器中查看我手动添加的项目):

<ItemsControl x:Name="itemCtrl" Style="{DynamicResource UsersControlStyle}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Top">
            </StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
</ItemsControl>


And this is my Template:

这是我的模板:

<Style x:Key="UsersControlStyle" TargetType="{x:Type ItemsControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ItemsControl}">
                <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                    <ScrollViewer VerticalScrollBarVisibility="Visible">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

回答by Oskar

To get a scrollbar for an ItemsControl, you can host it in a ScrollViewerlike this:

要获取 的滚动条ItemsControl,您可以ScrollViewer像这样托管它:

<ScrollViewer VerticalScrollBarVisibility="Auto">
  <ItemsControl>
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
  </ItemsControl>
</ScrollViewer>

回答by Andrey Shvydky

You have to modify the control template instead of ItemsPanelTemplate:

您必须修改控件模板而不是 ItemsPanelTemplate:

<ItemsControl >
    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

Maybe, your code does not working because StackPanel has own scrolling functionality. Try to use StackPanel.CanVerticallyScrollproperty.

也许,您的代码不起作用,因为 StackPanel 有自己的滚动功能。尝试使用StackPanel.CanVerticallyScroll属性。

回答by Patatrack

Put your ScrollViewer in a DockPanel and set the DockPanel MaxHeight property

将您的 ScrollViewer 放在 DockPanel 中并设置 DockPanel MaxHeight 属性

[...]
<DockPanel MaxHeight="700">
  <ScrollViewer VerticalScrollBarVisibility="Auto">
   <ItemsControl ItemSource ="{Binding ...}">
     [...]
   </ItemsControl>
  </ScrollViewer>
</DockPanel>
[...]