简体   繁体   English

UWP中的ListView不调整大小

[英]ListView Not Resizing in UWP

I have a listview like this. 我有这样的列表视图。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{x:Bind announcements}" IsItemClickEnabled="True" ItemClick="Announcement_ItemClick">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:Announcement">
                <StackPanel Orientation="Horizontal" Margin="0,20,0,0">
                    <StackPanel Height="Auto" Orientation="Vertical" Width="Auto">
                        <TextBlock Text="{x:Bind dates}" FontWeight="Bold"/>
                        <TextBlock Height="Auto" Text="{x:Bind titles}" Margin="0,6,0,0" TextWrapping="WrapWholeWords"/>
                    </StackPanel>
                    <StackPanel VerticalAlignment="Center">
                        <SymbolIcon Symbol="Download" Margin="20,0,0,0" Visibility="{x:Bind visibility}"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

The problem is if i have two stackpanels inside the main stackpanel the listview doesnt resize if the width changes. 问题是,如果我在主堆栈面板中有两个堆栈面板,并且宽度发生变化,则列表视图不会调整大小。 So i am not able to see the text in those textboxes. 所以我看不到那些文本框中的文本。

在此处输入图片说明

But if i remove the stackpanel in which symbolicon is placed then resizing takes place. 但是,如果我删除放置symbolicon的堆栈面板,则会发生大小调整。 Code for the listview without second stackpanel. 没有第二个stackpanel的listview的代码。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{x:Bind announcements}" IsItemClickEnabled="True" ItemClick="Announcement_ItemClick">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:Announcement">
                    <StackPanel Height="Auto" Margin="0,20,0,0" Orientation="Vertical" Width="Auto">
                        <TextBlock Text="{x:Bind dates}" FontWeight="Bold"/>
                        <TextBlock Height="Auto" Text="{x:Bind titles}" Margin="0,6,0,0" TextWrapping="WrapWholeWords"/>
                    </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

在此处输入图片说明

Is it possible to make the text in the textboxes get wrapped also with the download icon in my second stackpanel displayed. 是否可以使文本框中的文本也与第二个显示面板中的下载图标一起包装。 I want the download icon only in the right hand side of the item. 我只希望下载图标位于项目的右侧。 Is there any way to acheive this? 有什么办法可以做到这一点? Sorry for the long Question. 对不起,很长的问题。

The problem comes from your first horizontal stack panel. 问题来自您的第一个水平堆叠面板。 It basically says layout the children controls without any limit on the right so it will size itself to fit your "long"texts. 它基本上说对子控件进行布局时,其右边没有任何限制,因此可以调整其大小以适合您的“长”文本。 You can restrict this by using a more "strict" layout that will limit your items width to the listview width. 您可以通过使用更“严格”的布局来限制此设置,该布局会将您的项目宽度限制为listview宽度。

Instead of using the flexible StackPanel, you can use a Grid to force the children controls to remains within the parent bounds. 代替使用灵活的StackPanel,您可以使用Grid强制子控件保持在父边界之内。

<ListView.ItemTemplate>
 <DataTemplate x:DataType="local:Announcement">

 <Grid Margin="0,20,0,0" >**
   <Grid.ColumnDefinitions>
     <ColumnDefinition Width="*" />
     <ColumnDefinition Width="Auto" />
   </Grid.ColumnDefinitions>

   <StackPanel Height="Auto" Orientation="Vertical" Width="Auto">
     <TextBlock Text="{x:Bind dates}" FontWeight="Bold"/>
     <TextBlock Height="Auto" Text="{x:Bind titles}" Margin="0,6,0,0" TextWrapping="WrapWholeWords"/>
   </StackPanel>

   <StackPanel VerticalAlignment="Center" Grid.Column="1">
     <SymbolIcon Symbol="Download" Margin="20,0,0,0" Visibility="{x:Bind visibility}"/>
   </StackPanel>
 </Grid>
</DataTemplate>
</ListView.ItemTemplate>

This will give you the following: 这将为您提供以下内容: 布局结果

You can also force your download buttons to align to the right by changing the ItemContainerStyle. 您也可以通过更改ItemContainerStyle来使下载按钮向右对齐。 We request the list view items to stretch within the list view. 我们要求列表视图项在列表视图中延伸。 The default is to left align. 默认为左对齐。

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

在此处输入图片说明

Try replace StackPanel to Grid 尝试将StackPanel替换为Grid

<Grid Margin="0,20,0,0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Grid Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="{x:Bind dates}"
                    FontWeight="Bold"/>
        <TextBlock Grid.Row="1"
                    Height="Auto"
                    Text="{x:Bind titles}"
                    Margin="0,6,0,0"
                    TextWrapping="WrapWholeWords"/>
    </Grid>

    <SymbolIcon Symbol="Download" 
                Margin="20,0,0,0" 
                Grid.Column="1"
                Visibility="{x:Bind visibility}"/>
</Grid>

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

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