如何在列表视图中划分网格,在UWP


<Grid Grid.Row="3" HorizontalAlignment="Stretch">
                <ListView x:Name="lvAlert" HorizontalAlignment="Stretch">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="70*"/>
                                    <ColumnDefinition Width="30*"/>
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0" Background="{Binding ColorValue }" >
                                    <TextBlock Text="{Binding AlertType}" Foreground="White"  Height="35" HorizontalAlignment="Stretch"/>
                                </Grid>
                                <Grid Grid.Column="1"  Background="{Binding ColorValue }" >
                                    <TextBlock  Text="{Binding AlertTypeValue}" Foreground="Black" Height="35" HorizontalAlignment="Stretch"/>
                                </Grid>
                                <TextBlock Grid.Column="0" Text="{Binding AlertType}" Foreground="{Binding ColorValue }"  Width="400" Height="40"/>
                                <TextBlock Grid.Column="1" Text="{Binding AlertTypeValue}" Foreground="{Binding ColorValue }" Width="400" Height="40"/>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>

当我运行上面的代码时,我得到了一个输出,就像在单行中一样,没有得到70%和30%的正确分离。有人能解决这个问题吗?

默认情况下,ListView的项不会水平拉伸,而是保持在左侧。

你的问题应该由这段代码来解决:

 <ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
       <Setter Property="HorizontalContentAlignment"
               Value="Stretch" />
    </Style>
 </ListView.ItemContainerStyle>

当我运行上面的代码时,我得到了一个输出,就像在单行中一样,没有得到70%和30%的正确分离。有人能解决这个问题吗?

这是因为ColumnDefinitionsHeight应该设置为7*3*,而不是70*30*:

<Grid.ColumnDefinitions>
     <ColumnDefinition Width="7*"/>
     <ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>

有关网格使用的详细信息,请参阅网格类。

最新更新