如何在gridview中添加列标题 - UWP

时间:2017-10-27 04:29:44

标签: c# gridview uwp uwp-xaml

我想以表格格式显示动态数据。它应该有每列的列标题。如何将Gridview列标题添加到此?并且还需要为列行分隔符添加行。似乎Gridview控件在asp.net网站表单中更灵活。




 < StackPanel>
 <网格和GT;
 < Grid.ColumnDefinitions>
 < ColumnDefinition Width =“200”/>
 < ColumnDefinition Width =“200”/>
 < ColumnDefinition Width =“200”/>
 < ColumnDefinition Width =“200”/>
 < /Grid.ColumnDefinitions>

 < TextBlock Grid.Column =“0”Text =“Header”/>
 < TextBlock Grid.Column =“1”Text =“Header”/>
 < TextBlock Grid.Column =“2”Text =“Header”/>
 < TextBlock Grid.Column =“3”Text =“Header”/>
 < /网格和GT;
 <&的ScrollViewer GT;
 <网格和GT;
 < Grid.ColumnDefinitions>
 < ColumnDefinition Width =“200”/>
 < ColumnDefinition Width =“200”/>
 < ColumnDefinition Width =“200”/>
 < ColumnDefinition Width =“200”/>
 < /Grid.ColumnDefinitions>
 < Grid.RowDefinitions>
 < RowDefinition>< / RowDefinition>
 < RowDefinition>< / RowDefinition>
 < RowDefinition>< / RowDefinition>
 < /Grid.RowDefinitions>
 < TextBlock Grid.Column =“0”Text =“{Binding Name}”Foreground =“Blue”FontSize =“20”FontWeight =“ExtraBold”/>
 < TextBlock Grid.Column =“1”Text =“{Binding ID}”Foreground =“Blue”FontSize =“20”FontWeight =“ExtraBold”/>
 < TextBlock Grid.Column =“2”Text =“{Binding RollNo}”Foreground =“Blue”FontSize =“20”FontWeight =“ExtraBold”/>
 < TextBlock Grid.Column =“3”Text =“{Binding Division}”Foreground =“Blue”FontSize =“20”FontWeight =“ExtraBold”/>

 < /网格和GT;
 < /&的ScrollViewer GT;
< / StackPanel中>
  



1 个答案:

答案 0 :(得分:0)

我会这样做:

1)创建垂直StackPanel。

2)为标题

创建一个gridlayout

3)创建滚动视图

4)为数据创建一个gridlayout(嵌套在scrollview中)

在suppo xml:

 <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="Header"/>
            <TextBlock Grid.Column="1" Text="Header"/>
            <TextBlock Grid.Column="2" Text="Header"/>
            <TextBlock Grid.Column="3" Text="Header"/>
        </Grid>
        <ScrollViewer>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="Data"/>
                <TextBlock Grid.Row="0" Grid.Column="1" Text="Data"/>
                <TextBlock Grid.Row="0" Grid.Column="2" Text="Data"/>
                <TextBlock Grid.Row="0" Grid.Column="3" Text="Data"/>

                <TextBlock Grid.Row="1" Grid.Column="0" Text="Data"/>
                <TextBlock Grid.Row="1" Grid.Column="1" Text="Data"/>
                <TextBlock Grid.Row="1" Grid.Column="2" Text="Data"/>
                <TextBlock Grid.Row="1" Grid.Column="3" Text="Data"/>

                <TextBlock Grid.Row="2" Grid.Column="0" Text="Data"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Text="Data"/>
                <TextBlock Grid.Row="2" Grid.Column="2" Text="Data"/>
                <TextBlock Grid.Row="2" Grid.Column="3" Text="Data"/>                 
            </Grid>
        </ScrollViewer>
    </StackPanel>

更新了代码

  <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="Header"/>
            <TextBlock Grid.Column="1" Text="Header"/>
            <TextBlock Grid.Column="2" Text="Header"/>
            <TextBlock Grid.Column="3" Text="Header"/>
        </Grid>
        <ScrollViewer>
            <GridView ItemsSource="{Binding}" >
                <GridView.ItemTemplate>
                    <DataTemplate x:DataType = "local:student" >  <!--local:student is your class model to bind -->
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="200"/>
                                <ColumnDefinition Width="200"/>
                                <ColumnDefinition Width="200"/>
                                <ColumnDefinition Width="200"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" Text="{Binding ID }" Foreground="Blue" FontSize="20" FontWeight="ExtraBold"/>
                            <TextBlock Grid.Column="1" Text="{Binding Name}" Foreground="Blue" FontSize="20" FontWeight="ExtraBold"/>
                            <TextBlock Grid.Column="2" Text="{Binding RollNumber}" Foreground="Blue" FontSize="20" FontWeight="ExtraBold"/>
                            <TextBlock Grid.Column="3" Text="{Binding Division}" Foreground="Blue" FontSize="20" FontWeight="ExtraBold"/>

                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </ScrollViewer>
    </StackPanel>