【问题标题】:Button after ListBox列表框后的按钮
【发布时间】:2013-04-10 13:04:39
【问题描述】:

我需要显示带有动态内容的ListBox(所以我不知道它的高度)和下面的Button。所以用户应该能够滚动 ListBox 以结束并查看按钮。

在 Android 中,我会使用 RelativeLayoutbelow 属性作为按钮或其他解决方案,但在 WP 中我不知道该怎么做。

我尝试过的:

1) 全部输入StackPanel

<StackPanel>
    <ListBox />
    <Button />
</StackPanel>

这不起作用,因为 StackPanel 阻止了 ListBox 滚动。

2) 好的,让我们把 ListBox 放到 Grid 中

<StackPanel>
    <Grid>
        <ListBox />
    </Grid>
    <Button />
</StackPanel>

什么都没有发生。

3) 全部放入 Grid 并使用 Grid.Row 无效。

4) 让我们将所有内容放入Grid 并动态设置Button 的边距

<Grid>
    <ListBox />
    <Button />
</Grid>

好的,这是可行的,但这不是一个好的解决方案,因为我需要每次处理ListBoxpopulating 并重置按钮的边距。坏坏坏。

附:另外,我可以将按钮作为 ListBox 项(不错,但不好:)

请帮帮我...

【问题讨论】:

    标签: silverlight windows-phone-7 windows-phone-8 windows-phone


    【解决方案1】:

    如果我理解正确,您需要一个简单的 Grid 定义如下:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox />
        <Button Grid.Row="1" Height="80"/>
    </Grid>
    

    通过将第 0 行的高度设为“星形”并将第一行设为 Auto,ListBox 将填充剩余空间,而 Button 仅为 80(您可以将其更改为您喜欢的任何值)。 ListBox 会自动放到第 0 行,因为如果没有明确设置,这是默认设置。

    如果您不希望按钮固定在页面上而是使用 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,0,12,0">
        <ListBox Style="{StaticResource ListBoxWithButtonStyle}">
    
        </ListBox>
    </Grid>
    

    【讨论】:

    • 感谢您的回答,我已经尝试过您的解决方案,但Button 始终可见,我只需要在用户滚动ListBox 以结束时显示按钮,就像按钮是否是最后一个一样列表框项
    • 哇,有大量代码可以轻松完成任务 :) 为什么 Silverlight 如此丑陋...好吧,我会试试看 :)
    • 实际上并没有那么糟糕。模板代码已经差不多了,你需要做的就是改变 ScrollViewer 里面的内容,这只是 3-4 行代码!
    • 好吧,如果您在 Page 资源中声明了模板,您只需添加 Click 事件处理程序并在后面的 Page.xaml.cs 代码中处理它!
    • 我已将样式移至页面 xaml,谢谢!但我仍然认为silverlight很丑:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多