【问题标题】:Scrolling on ListBox in Popup在弹出窗口中滚动列表框
【发布时间】:2015-10-13 15:01:56
【问题描述】:

我有那个布局:

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

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

为什么滚动只在 ListBox 上方不起作用?

【问题讨论】:

  • 当您滚动到 ListBox 时,您会滚动到 ListBox。我假设您在谈论鼠标滚轮滚动。
  • 你有一个 winrt/universal 或 Windows phone 项目而不是 WPF 吗?

标签: c# wpf xaml listbox popup


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 2014-10-29
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多