How do I dynamically size a GridView Item?

2019-05-05 19:27发布

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:

enter image description here

How do I dynamically size the items within the GridView?

1条回答
你好瞎i
2楼-- · 2019-05-05 19:39

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>
查看更多
登录 后发表回答