简体   繁体   English

使用Windows Phone 7的Silverlight创建表

[英]Creating a table with Silverlight for Windows Phone 7

I'd like to create a table on WP7. 我想在WP7上创建一个表格。 This is my current approach using a ListBox with a Grid as the data template. 这是我目前使用带有网格的ListBox作为数据模板的方法。

<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>

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. 这意味着,如果textBlock1中有长文本,则列0将更大。 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. 在下一行中,textBlock1中的文本可能更短,导致列0也比上一行中的列0短。

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. 我不想使用固定宽度,因为当方向从纵向更改为横向时,列会自动调整大小。

There is the HeaderedItemsControl, but as I understand it it is not available for Windows Phone 7? 有HeaderedItemsControl,但据我了解,它不适用于Windows Phone 7?

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. 在WPF中,存在SharedSizeGroup的概念,该概念允许您在多个网格之间共享列宽,但这在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://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 http://databaseconsultinggroup.com/blog/2009/05/simulating_sharedsizegroup_in.html

Although neither are simple solutions. 尽管都不是简单的解决方案。

You might also try Mike's AutoGrid: 您也可以尝试Mike的AutoGrid:

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

Here is my solution using SharedSizeGroup as suggested by ColinE. 这是ColinE建议的使用SharedSizeGroup的解决方案。

<ListBox x:Name="ResultsList">

    <ListBox.Resources>
        <SharedSize:SharedSizeGroup x:Key="Col1Width" />
        <SharedSize:SharedSizeGroup x:Key="Col2Width" />
        <SharedSize:SharedSizeGroup x:Key="Col3Width" />

        <DataTemplate x:Key="ResultsListItem">
            <StackPanel d:DesignWidth="385" Orientation="Horizontal">
                <SharedSize:SharedSizePanel WidthGroup="{StaticResource Col1Width}">
                    <TextBlock x:Name="textBlock" MaxWidth="100" Text="{Binding A}"/>
                </SharedSize:SharedSizePanel>
                <SharedSize:SharedSizePanel WidthGroup="{StaticResource Col2Width}">
                    <TextBlock x:Name="textBlock1" MaxWidth="85" Text="{Binding B}"/>
                </SharedSize:SharedSizePanel>
                <SharedSize:SharedSizePanel WidthGroup="{StaticResource Col3Width}">
                    <TextBlock x:Name="textBlock2" MaxWidth="200" Text="{Binding C}"/>
                </SharedSize:SharedSizePanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <StaticResource ResourceKey="ResultsListItem"/>
    </ListBox.ItemTemplate>
</ListBox>

Even the maximum with of each column can be controlled via the TextBlock's MaxWidth property. 甚至每列的最大值都可以通过TextBlock的MaxWidth属性来控制。 The SharedSizeGroups ensure that the TextBlocks have the same size in each row. SharedSizeGroups确保TextBlocks在每一行中具有相同的大小。

You can use WrapPanel . 您可以使用WrapPanel Set the following ItemsPanel in the Datatemple , you can just have textblock. 设置以下ItemsPanelDatatemple ,你可以有文字块。

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <control:WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM