使用 Silverlight for Windows Phone 7 创建表格 [英] Creating a table with Silverlight for Windows Phone 7

查看:30
本文介绍了使用 Silverlight for Windows Phone 7 创建表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WP7 上创建一个表.这是我目前使用带有网格的 ListBox 作为数据模板的方法.

I'd like to create a table on WP7. This is my current approach using a ListBox with a Grid as the data template.

<ListBox x:Name="ResultsList" Margin="12,0" Grid.Row="1">
    <ListBox.Resources>
        <DataTemplate x:Key="ResultsListItem">
            <Grid d:DesignWidth="385" Height="28">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="88"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="textBlock1" Margin="0,0,24,0"/>
                <TextBlock x:Name="textBlock2" Margin="0,0,24,0"
                    VerticalAlignment="Top" Grid.Column="1"/>
                <TextBlock x:Name="textBlock3" Margin="0,0,24,0" 
                    VerticalAlignment="Top" Grid.Column="3"/>
            </Grid>
        </DataTemplate>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <StaticResource ResourceKey="ResultsListItem"/>
    </ListBox.ItemTemplate>
</ListBox>

问题是,结果表的列大小不同.网格的列定义独立于其他行应用于每一行.这意味着,如果 textBlock1 中有长文本,则第 0 列会更大.在下一行中,textBlock1 中的文本可能较短,导致第 0 列也比前一行中的第 0 列短.

The problem is, that the resulting table's columns are not sized equally. The Grid's column definitions are applied to each row independently of the other rows. That means, if there is a long text in textBlock1, column 0 will be larger. In the next row there could be a shorter text in textBlock1, resulting in column 0 also being shorter than the column 0 in the previous row.

如何使所有行中的列大小相同?我不想使用固定宽度,因为当方向从纵向变为横向时,列会自动调整大小.

How can the columns in all rows be sized equally? I don't want to use fixed width because when the orientation changes from portrait to landscape the colums would resize automatically.

有 HeaderedItemsControl,但据我所知它不适用于 Windows Phone 7?

There is the HeaderedItemsControl, but as I understand it it is not available for Windows Phone 7?

推荐答案

这是一个棘手的问题!在 WPF 中存在 SharedSizeGroup 的概念,它允许您跨多个网格共享列宽,但这在 Silverlight 中不可用.

This is a tricky problem! In WPF there exists the concept of a SharedSizeGroup, which allows you to share column widths across multiple grids, but this is not available in silverlight.

网络上有一些解决方法:

There are a few workarounds on the web:

http://www.scottlogic.co.uk/blog/colin/2010/11/using-a-grid-as-the-panel-for-an-itemscontrol/

http://databaseconsultinggroup.com/blog/2009/05/simulating_sharedsizegroup_in.html

虽然两者都不是简单的解决方案.

Although neither are simple solutions.

您也可以试试 Mike 的 AutoGrid:

You might also try Mike's AutoGrid:

http://whydoidoit.com/2010/10/06/automatic-grid-layout-for-silverlight/

这篇关于使用 Silverlight for Windows Phone 7 创建表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆