wpf 列表框中的滚动条不起作用

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

Scrollbar in Listbox not working

wpflistboxscrollbar

提问by Jim Reineri

I have a ListBoxthat displays a list of WPF controls. My problem is that the vertical scrollbar is show but is disabled even when there are enough items that the ListBoxshould be scrollable. One other possibly relevant fact is that this is contained in an Integration.ElementHost.

我有一个ListBox显示 WPF 控件列表的控件。我的问题是垂直滚动条显示但被禁用,即使有足够的项目ListBox应该可以滚动。另一个可能相关的事实是它包含在Integration.ElementHost.

WPF noobie, Jim

WPF 菜鸟,吉姆

Here is the XAML for the ListBox:

这是用于的 XAML ListBox

  // for brevity I removed the Margin and Tooltip attributes

  <Grid x:Class="Xyzzy.NoteListDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Name="stackPanel" Orientation="Vertical"
                ScrollViewer.VerticalScrollBarVisibility="Visible">
        <StackPanel Orientation="Horizontal">
            <CheckBox Name="AllRecent" IsChecked="False" >View All Recent</CheckBox>
            <CheckBox Name="AscendingOrder" IsChecked="False">Descending Order</CheckBox>
            <Button Name="btnTextCopy" Click="btnCopyText_Click">Copy All</Button>
        </StackPanel>
        <ListBox Name="NoteList"
                 ScrollViewer.CanContentScroll="True"
                 ScrollViewer.VerticalScrollBarVisibility="Visible">
        </ListBox>
      </StackPanel>
  </Grid>

And the XAML for the control displayed in each ListBoxitem:

以及每个ListBox项目中显示的控件的 XAML :

  <UserControl x:Class="Xyzzy.NoteDisplay"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
      <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
          <TextBlock Name="Heading" FontSize="10">Note Heading</TextBlock>
          <Button Name="btnCopyText" Height="20" FontSize="12"
                          Click="btnCopyText_Click">Copy
          </Button>
        </StackPanel>
        <TextBlock Name="Body" FontSize="14">Note Body</TextBlock>
      </StackPanel>
    </Grid>
  </UserControl>

回答by John Myczek

I have had problems with scroll bar visibility when using a StackPanel. I think it is because the StackPanelis always as big as it needs to be to contain all of its children. Try reorganizing the layout to remove the StackPanel(use a Gridinstead) and see if that helps.

使用StackPanel. 我认为这是因为StackPanel它总是尽可能大以容纳其所有子项。尝试重新组织布局以删除StackPanel(使用 aGrid代替),看看是否有帮助。

回答by Mert Akcakaya

You just need to introduce Heightproperty, like this:

你只需要引入Height属性,像这样:

<ListBox Height="200" 
         Name="NoteList"
         ScrollViewer.CanContentScroll="True"
         ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>

回答by Mert Akcakaya

Heya, I suspect what might be happening is that your ListBox is expanding enough for every item however the ListBox is actually disappearing off the bottom of the Containing Control.

嘿,我怀疑可能发生的情况是您的 ListBox 为每个项目扩展了足够多,但是 ListBox 实际上从 Containing Control 的底部消失了。

Does the ListBox actually stop properly or does it just seem to disappear? Try setting a MaxHeight on the ListBox and see if that makes the scrollbar appear. You should be able to set the VerticalScrollBarVisibility to Auto to have it only appear when needed.

ListBox 是否真的停止了,还是似乎消失了?尝试在 ListBox 上设置 MaxHeight 并查看是否会使滚动条出现。您应该能够将 VerticalScrollBarVisibility 设置为 Auto 使其仅在需要时出现。

回答by AustinTX

If the list box is inside a StackPanel, try these steps for your ListBox

如果列表框在 StackPanel 内,请为您的 ListBox 尝试这些步骤

  1. Set ScrollViewer.VerticalScrollBarVisibility="Auto"
  2. Setting the Height property of a ListBox to some height that you expect to see.
  1. 设置 ScrollViewer.VerticalScrollBarVisibility="Auto"
  2. 将 ListBox 的 Height 属性设置为您希望看到的某个高度。

That should force the scroll bar to show up.

这应该会强制显示滚动条。

回答by Rachael

This is pretty late, but anyone using ListBox probably shouldn't have it in a StackPanel. Once I switched the parent control of my Listbox from StackPanel to DockPanel with LastChildFill=True (Where the listbox was the last control), my scrollbar worked perfectly.

这已经很晚了,但任何使用 ListBox 的人可能都不应该在 StackPanel 中使用它。一旦我使用 LastChildFill=True(列表框是最后一个控件)将我的列表框的父控件从 StackPanel 切换到 DockPanel,我的滚动条就完美地工作了。

Hope this helps someone who's problem was not solved by the above answer.

希望这可以帮助那些没有通过上述答案解决问题的人。

回答by VcDeveloper

Another solution with a modification to Dave's is to use the ScrollViewer only. You only be able to scroll by placing your mouse on the ScrollView's ScrollBar. I use it this way because I don't like how ListBox jumps from item to item and sometimes missing items from the Top. Little bit hard on the eyes too. I like ScrollViewer's smooth scrolling.

另一个修改 Dave 的解决方案是仅使用 ScrollViewer。您只能通过将鼠标放在 ScrollView 的 ScrollBar 上来滚动。我这样使用它是因为我不喜欢 ListBox 如何从一个项目跳到另一个项目,有时会从顶部丢失项目。眼睛也有点硬。我喜欢 ScrollViewer 的平滑滚动。

回答by Noctis

I just ran into the same issue, and here's a little code demo on code project that visually shows it.

我刚刚遇到了同样的问题,这里有一个关于代码项目的小代码演示,直观地展示了它。

(If you want to save yourself the time of writing the code to see the differences yourself :) )

(如果您想节省自己编写代码的时间以自己查看差异:))

http://www.codeproject.com/Tips/659066/ListBox-and-Panels-in-WPF

http://www.codeproject.com/Tips/659066/ListBox-and-Panels-in-WPF

回答by AJH

Another solution to this problem that works well is to put a ScrollVieweraround the StackPanel.

此问题的另一个有效解决方案是ScrollViewerStackPanel.