固定XAML GridView中的宽度列

时间:2015-02-11 22:44:31

标签: c# gridview windows-store-apps winrt-xaml

我有一个XAML GridView,每行包含一个Grid。我试图像在HTML表格中那样将所有列对齐,但我没有运气。这是我的XAML

<Grid Grid.Row="1" Grid.ColumnSpan="2" Margin="20,0,20,0" Background="BlueViolet">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="8*"></ColumnDefinition>
        <ColumnDefinition Width="1*"></ColumnDefinition>
        <ColumnDefinition Width="1*"></ColumnDefinition>
        <ColumnDefinition Width="1*"></ColumnDefinition>
        <ColumnDefinition Width="1*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Text="Name" Margin="5" />
    <TextBlock Grid.Column="1" Text="WordCount" Margin="5" />
    <TextBlock Grid.Column="2" Text="Created" Margin="5" />
    <TextBlock Grid.Column="3" Text="Updated" Margin="5" />
    <TextBlock Grid.Column="4" Text="Version" Margin="5" />
</Grid>

<GridView Grid.Row="2" Grid.ColumnSpan="2" x:Name="Files_Grid" Margin="20,0,20,20" ItemsSource="{Binding ElementName=App_Frame, Path=DataContext.FilesCollection}" 
            ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.VerticalScrollBarVisibility="Visible" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch">
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid Tapped="Files_Grid_Row_Tapped" RightTapped="Files_Grid_Row_RightTapped" Tag="{Binding GUID}" Width="Auto" VerticalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="8*"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0" Visibility="{Binding Name_Edit_Visibility}" Margin="5" Orientation="Horizontal">
                    <TextBox Text="{Binding Name, Mode=TwoWay}"></TextBox>
                    <Button Tapped="Files_Grid_Row_Edit_Name_OK_Tapped">OK</Button>
                    <Button Tapped="Files_Grid_Row_Edit_Name_Cancel_Tapped">Cancel</Button>
                </StackPanel>
                <TextBlock Grid.Column="0" Text="{Binding LatestVersion.Name}" Margin="5"></TextBlock>
                <TextBlock Grid.Column="1" Text="{Binding LatestVersion.WordCount}" HorizontalAlignment="Right" Margin="5"></TextBlock>
                <TextBlock Grid.Column="2" Text="{Binding Created}" Margin="5" HorizontalAlignment="Right"></TextBlock>
                <TextBlock Grid.Column="3" Text="{Binding LatestVersion.Updated}" Margin="5" HorizontalAlignment="Right"></TextBlock>
                <TextBlock Grid.Column="4" Text="{Binding LatestVersion.VersionNumber}" Margin="5" HorizontalAlignment="Right"></TextBlock>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel HorizontalAlignment="Left" VerticalAlignment="Stretch" />
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="HorizontalContentAlignment"  Value="Stretch" />
            <Setter Property="VerticalContentAlignment"  Value="Stretch" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
        </Style>
    </GridView.ItemContainerStyle>
</GridView>

正如你所看到的,我已经尝试将VerticalAlignment和VerticalContentAlignment设置为我想到的任何地方,但是生成的gridview的每一行都有略微不同的列宽。

如何使每个GridView行具有相同的宽度列,而不实际设置固定宽度?

1 个答案:

答案 0 :(得分:1)

我不太确定我理解你的问题, 你想要对齐列的内容吗? 如果是这样尝试拉伸文本框 您是否尝试将标头GridGridView内容对齐? 如果是这样,尝试将标题数据与GridView数据一起添加到GridView