How do I dynamically size a GridView Item?

牧云@^-^@ 提交于 2019-12-07 08:32:26

问题


I have a Grid View that is used to display "tags" which is a list of strings that are dynamic in size. Using the following code:

<GridView ItemsSource="{Binding Tags}" 
          ItemTemplate="{StaticResource TagTemplate}" 
          VerticalAlignment="Bottom"
          Grid.RowSpan="2"
          SelectionMode="None"
          />

I use the following Template for the items:

<DataTemplate x:Name="TagTemplate">
    <Border BorderBrush="Gray" BorderThickness="1" Opacity="75">
        <TextBlock Text="{Binding}"/>
    </Border>
</DataTemplate>

When added to the Grid, the size of each of the items are the same size as the first:

How do I dynamically size the items within the GridView?


回答1:


So something like;

<ScrollViewer>
    <ItemsControl ItemsSource="{Binding Tags}">
        <!-- ItemsPanelTemplate -->
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
                <!-- Or use WrapPanel depending on its display -->
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <!-- ItemContainerStyle -->
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Gray" BorderThickness="1" Opacity="75" Padding="3" Margin="3,0">
                     <TextBlock Text="{Binding}"/>
                </Border>                             
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>


来源:https://stackoverflow.com/questions/13254107/how-do-i-dynamically-size-a-gridview-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!