我需要向ListBox显示动态内容(所以我不知道它的高度)和下面的Button.因此用户应该能够将ListBox滚动到结束并查看按钮.
在Android中,我会使用RelativeLayout和以下属性作为按钮或其他解决方案,但在WP中我不知道该怎么做.
我曾经尝试过的:
1)全部放入StackPanel
<StackPanel> <ListBox /> <Button /> </StackPanel>
这不起作用,因为StackPanel阻止了ListBox滚动.
2)好的,让我们把ListBox放在Grid中
<StackPanel> <Grid> <ListBox /> </Grid> <Button /> </StackPanel>
什么都没发生.
3)全部放入网格并使用Grid.Row不起作用.
4)让我们全部放入网格并动态设置Button的边距
<Grid> <ListBox /> <Button /> </Grid>
好的,这是有效的,但这不是好的解决方案,因为我需要每次都处理ListBoxpopulating并重置按钮的边距.坏坏.
附:另外,我可以将按钮作为ListBox项目(不错,但不是很好:)
请帮帮我…
解决方法
如果我理解正确,你需要一个像这样定义的简单网格:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <ListBox /> <Button Grid.Row="1" Height="80"/> </Grid>
通过将行的高度设置为“星形”并将第一行设置为“自动”,ListBox将填充剩余空间,而“按钮”仅为80(您可以将其更改为您喜欢的任何内容). ListBox会自动放入零行,因为如果未明确设置,则为默认值.
如果您不希望按钮固定在页面上但使用ListBox滚动,则可以编辑ListBox模板,如下所示:
<Style x:Key="ListBoxWithButtonStyle" TargetType="ListBox"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Padding" Value="0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBox"> <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ItemsPresenter/> <Button Content="Touch me" Height="80" Grid.Row="1"/> </Grid> </ScrollViewer> </ControlTemplate> </Setter.Value> </Setter> </Style>
然后应用于ListBox:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,12,0"> <ListBox Style="{StaticResource ListBoxWithButtonStyle}"> </ListBox> </Grid>