在 Popup 的 ListBox 上滚动

Scrolling on ListBox in Popup

我有那个布局:

<Popup>
    <Border>
        <ScrollViewer>
            <Grid>
                <TextBlock/>
                <ListBox/>
                <TextBlock/>
                <ListBox/>
            </Grid>
        </ScrollViewer>
    </Border>
</Popup>

我遇到的问题是,当光标位于 TextBlock 和边框背景上方时我可以滚动,但当它位于 ListBox 项目上方时我不能。 ListBox 显示没有滚动条的所有项目。 我希望整个 ListBox 表现为一个控件。

为什么滚动只在列表框上方不起作用?

ScrollViewerListBox 都支持滚动。 您的 Grid 将允许其中的所有控件在分配给它的尺寸内尽可能多地扩展。在这种情况下,维度在功能上是无限的,因为 Grid 被包裹在 ScrollViewer 中。如果您希望 ListBox 滚动而不是完全展开,您需要在 Grid 上设置尺寸,或者删除 ScrollViewer.

尺寸集:

<Popup>
    <Border>
        <ScrollViewer>
            <Grid Height="200" Width="200">
                <TextBlock/>
                <ListBox/>
                <TextBlock/>
                <ListBox/>
            </Grid>
        </ScrollViewer>
    </Border>
</Popup>

没有ScrollViewer:

<Popup>
    <Border>
        <Grid>
            <TextBlock/>
            <ListBox/>
            <TextBlock/>
            <ListBox/>
        </Grid>
    </Border>
</Popup>

如果滚动是指鼠标滚轮,那么它与 ListBoxPopup 中无关。这是因为 ScrollViewerListBox 默认模板的一部分。如果你想在其他地方处理滚动,你需要通过更改 ListBox.Template

来删除 ScrollViewer
<ListBox>
    <ListBox.Template>
        <ControlTemplate TargetType="{x:Type ListBox}">
            <ItemsPresenter/>
        </ControlTemplate>
    </ListBox.Template>
</ListBox>