Windows 8手机中的多列列表

时间:2014-02-03 09:29:53

标签: c# windows-phone-8

我正在尝试创建一个应用程序,以在Windows 8手机中显示表格列表(ID,名称,Lname)。 我的XAML文件如下                                                                                        

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"  />
                <ColumnDefinition Width="1*"  />
                <ColumnDefinition Width="1*"  />
            </Grid.ColumnDefinitions>

            <Grid.Resources>
                <Style x:Key="ListBoxStyle" TargetType="ListBoxItem">
                    <Setter Property="Background" Value="{StaticResource PhoneSemitransparentBrush}" />
                    <Setter Property="Margin" Value="3,5" />
                    <Setter Property="FontSize" Value="20" />
                    <Setter Property="BorderBrush" Value="{StaticResource PhoneBorderBrush}" />
                    <!-- Replace the default item template with a basic template that does not highlight selected items. -->
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <ContentPresenter/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <Style x:Key="SummaryStyle" TargetType="TextBlock">
                    <Setter Property="Margin" Value="5" />
                    <Setter Property="Width" Value="75" />
                    <Setter Property="HorizontalAlignment" Value="Left" />
                </Style>
            </Grid.Resources>


            <StackPanel Orientation="Horizontal">
                <TextBlock  Text="ID" Width="100" />
                <TextBlock  Text="Name" Width="150"/>
                <TextBlock  Text="LName"  Width="150"/>
            </StackPanel>

            <ScrollViewer Margin="-5,13,3,36" Height="558">
                <ListBox Name="lstBox" 
          ItemsSource="{Binding}" 
          Height="380" HorizontalAlignment="Left" Margin="5,25,0,0" 
          VerticalAlignment="Top" Width="444" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding ID}" />
                                <TextBlock Text="{Binding FName}" />
                                <TextBlock Text="{Binding LName}" />

                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </ScrollViewer>

        </Grid>

我在按钮上点击下面的代码甚至点击

        List<Customers> cust = new List<Customers>();
        cust.Add(new Customers(1, "Ganesh", "S"));
        cust.Add(new Customers(2, "Shan", "S"));
        cust.Add(new Customers(3, "Anjan", "A"));
        lstBox.ItemsSource = cust;

但是,它不显示它只显示列的列表。这些客户名称应显示在Windows 8手机中


ID名称LName


1                  Ganesh                   S
2                  Shan                     S
3                  Anjan                    A

代码中缺少什么?或者在Windows 8手机中显示多列列表时应该注意什么?

2 个答案:

答案 0 :(得分:0)

您已定义了三列,但StackPanel位于第一列(默认行为),因此我猜您无法查看数据。

或者您可以将标题TextBlocks分配给网格(删除堆栈面板),并在itemtemplate中定义另一个宽度相似的网格,这样可以帮助您实现布局。

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="1*"  />
  <ColumnDefinition Width="1*"  />
  <ColumnDefinition Width="1*"  />
</Grid.ColumnDefinitions>

<TextBlock  Text="ID" Width="100" Grid.Column="0"/>
<TextBlock  Text="Name" Width="150" Grid.Column="1"/>
<TextBlock  Text="LName"  Width="150" Grid.Column="2"/>

然后你的模板就像

一样
<ListBox.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
      <ColumnDefinition Width="1*"  />
      <ColumnDefinition Width="1*"  />
      <ColumnDefinition Width="1*"  />
</Grid.ColumnDefinitions>
     <TextBlock Text="{Binding ID}" Grid.Column="0"/>
     <TextBlock Text="{Binding FName}" Grid.Column="1"/>
     <TextBlock Text="{Binding LName}" Grid.Column="2"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>

(未根据您的帖子重新安排测试代码)希望这有帮助

答案 1 :(得分:0)

Grid内创建StackPanel。在其中创建一个column

这看起来像:

<ScrollViewer Margin="-5,13,3,36" Height="558">
            <ListBox Name="lstBox" 
      ItemsSource="{Binding}" 
      Height="380" HorizontalAlignment="Left" Margin="5,25,0,0" 
      VerticalAlignment="Top" Width="444" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                           <Grid>
                               <Grid.ColumnDefinitions>
                                  <ColumnDefinition Width="Specify width"  />
                                  <ColumnDefinition Width="Specify width"  />
                                  <ColumnDefinition Width="Specify width"  />
                               </Grid.ColumnDefinitions>

                               <TextBlock Text="{Binding ID}" Grid.Column="0" />
                               <TextBlock Text="{Binding FName}" Grid.Column="1" />
                               <TextBlock Text="{Binding LName}" Grid.Column="2" />
                           </Grid> 
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </ScrollViewer>