【问题标题】:Two components in the same .xaml Grid.Row. When I scroll down, one of them floats, the other scrolls. I need both of them to scroll同一 .xaml Grid.Row 中的两个组件。当我向下滚动时,其中一个浮动,另一个滚动。我需要他们两个滚动
【发布时间】:2015-03-13 04:02:07
【问题描述】:

我遇到了一些需要修复的 .xaml 代码。目前,它由具有此布局的 2 个网格组件组成:

<Grid d:SomeDataContext>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ListView Style="{StaticResource SomeListStyle}" Grid.Row="0" Margin="0,0,0,0" Grid.RowSpan="2">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical" Margin="0,0,0,80" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <mycontrol:Panel1Control />
        <mycontrol:Panel2Control />
        <mycontrol:Panel3Control />
        <mycontrol:Panel4Control />
        <mycontrol:Panel5Control />
    </ListView>
    <controls:HeaderControlTransparent Grid.Row="0" />
</Grid>

当这个 xaml 被解释时,在运行时你会得到一个很好的列表滚动条,它由 Panel1、Panel2 等组成……除此之外,还有一些与屏幕顶部对齐的透明标题。 事实上,这个标题是“如此对齐”的,它在滚动时不会移动,而是浮动。 所需的位置类似于:

[Header]
[Panel1]
[Panel2]
[Panel3]
[Panel4]
[Panel5]

当有人滚动时,期望的行为应该是从屏幕上部消失的 [header] 元素,就好像它是其他面板之一一样。

Desired (not happening)
...     
[Panel3]
[Panel4]
[Panel5]


Undesired (happening)
[Header]
[Panel4]
[Panel5]

目前标题不滚动,它只是浮​​动在所有内容的顶部,与屏幕顶部对齐,而面板滚动。

我应该解决的任何提示?我认为一切都井井有条。我不明白为什么一排网格滚动而另一排不滚动。

我对 .xaml 有点陌生,所以这也可能是我遇到麻烦的原因之一。 谢谢。

【问题讨论】:

  • 因此,只需将您的 ListView 更改为 Grid.Row=1,并禁用 ListView 上的垂直/水平滚动,然后将其全部放入 ScrollViewer,就完成了。
  • ScrollViewer 做到了。 Grid.Row=0 (所以重叠)有点有意,所以我离开了它,标题出现在第一个面板的顶部(它有空间)。顺便说一句,如果您将评论更改为正确的答案,我会投票给您。

标签: wpf xaml grid-layout


【解决方案1】:

因此不妨在评论中添加一些内容,以便为未来的读者提供一些额外的解释,并且显然是一些简单的要点。

在您的示例中,Grid 有两行(我猜是其他原因),两个孩子都在同一个单元格中。由于 Header 控件部分位于 DOM 中的 ListView 下方,因此它在逻辑上呈现在底层 ListView 之上。

一个 ListView 已经作为 ScrollViewer 内置在控件模板中以嵌套其项目。但是 OP 要求控件模板之外的元素与 ListView 中的项目一起滚动。

因此,通过在它们自己的父 ScrollViewer 中以正确的顺序嵌入 ListView 和 Header 控件,可以得到滚动两个内容位的预期结果。

例如(伪)

<Grid>
  <ScrollViewer>
    <objects2/>
  </ScrollViewer>
  <object1/>
</Grid>

将对象 1 浮动到对象 2 的顶部并保持对象 1 静止,而其他一切都滚动。但是;

<ScrollViewer>
   <Grid>
     <Grid.RowDefinitions>
       <RowDefinition/>
       <RowDefinition Height="*"/>
     </Grid.RowDefinitions>
     <object1/>
     <objects2 Grid.Row="1"/>
   </Grid>
</ScrollViewer>

将滚动所有保持其堆叠位置的孩子。

希望这会有所帮助,干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多