简体   繁体   English

UWP中的ListView

[英]ListView in UWP

I am trying to Align my ListView items in dynamic way without setting explicitly Width for them. 我试图以动态方式对齐我的ListView项目,而不为它们明确设置Width。 The problem i am getting is aligning them the attribute name that is above them. 我得到的问题是将它们与它们上面的属性名称对齐。 ie attribute Name should have Coke below it, Quantity should have Integer value number and Subtotal should have floating points below it. 即属性Name应该在它下面有Coke,Quantity应该有Integer值数字,Subtotal应该在它下面有浮点数。 if i resize my Windows the ListView should also change with the respective attribute name but it doesn't. 如果我调整Windows的大小,ListView也应该使用相应的属性名称进行更改,但事实并非如此。 I have tried RelativePanel, ListBox for them but nothing works without explicity giving a width which i want it to be dynamic. 我已经为他们尝试了RelativePanel,ListBox,但是没有任何效果,没有明确给出宽度,我希望它是动态的。

<Grid Grid.Column="2" Margin="5,0,5,0">

            <ScrollViewer VerticalScrollBarVisibility="Hidden">
                <StackPanel>
                    <TextBlock Style="{StaticResource MyTextBlock1}" FontSize="20" HorizontalAlignment="Center" Margin="0,0,0,10">Order Summary</TextBlock>
                    <RelativePanel>
                        <TextBlock Name="ItemName" Style="{StaticResource MyTextBlock1}" FontSize="12">Name</TextBlock>
                        <TextBlock Name="ItemPrice" Style="{StaticResource MyTextBlock1}" FontSize="12" RelativePanel.AlignRightWithPanel="True">SubTotal</TextBlock>
                        <TextBlock Name="ItemQuantity" Style="{StaticResource MyTextBlock1}" Margin="0,0,10,0" FontSize="12" RelativePanel.LeftOf="ItemPrice">Quantity</TextBlock>
                    </RelativePanel>

                    <ListView ItemsSource="{x:Bind dumbList}">
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="local:Dumb">
                                <Grid Width="200">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="3*"/>
                                        <ColumnDefinition Width="1*"/>
                                        <ColumnDefinition Width="1*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{x:Bind name}"/>
                                    <TextBlock Text="{x:Bind id}" Grid.Column="1"/>
                                    <TextBlock Name="Quatity" Text="{x:Bind price}" Grid.Column="2"/>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
</Grid>

CLICK HERE!! 点击这里!! for the image 对于图像

Edit: 编辑:

You can create one item with the DataTemplate layout above the listview. 您可以使用listview上方的DataTemplate布局创建一个项目。

To avoid setting explicitly the Width, you can set the HorizontalAlignment to Stretch (similiar to Android XML fill_parent) and use Margin. 要避免明确设置宽度,可以将Horizo​​ntalAlignment设置为Stretch(类似于Android XML fill_parent)并使用Margin。

Example: 例:

<Grid 
    HorizontalAlignment="Stretch" 
    Margin="12,12,12,12"
    Height="100">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Name"/>
    <TextBlock Text="Quantity" Grid.Column="1"/>
    <TextBlock Text="SubTotal" Grid.Column="2"/>
</Grid>

<ListView ItemsSource="{x:Bind dumbList}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:Dumb">
            <Grid
                HorizontalAlignment="Stretch" 
                Margin="12,12,12,12"
                Height="100">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="3*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{x:Bind name}"/>
                <TextBlock Text="{x:Bind id}" Grid.Column="1"/>
                <TextBlock Name="Quatity" Text="{x:Bind price}" Grid.Column="2"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Your column definitions should be percentages, not whole values. 您的列定义应为百分比,而不是整数值。

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

.8 = 80% .1 = 10% .1 = 10% Total = 100% .8 = 80% .1 = 10% .1 = 10% 总计= 100%

Your definitions are: 你的定义是:

3 = 300% 1 = 100% 1 = 100% Total = 500% !! 3 = 300% 1 = 100% 1 = 100% 总计= 500%!!

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

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