C# 如何为gridview项(文本块)指定动态宽度?
我正在开发一个Windows10UWP和一个WindowsPhone8.1项目。在该项目中,有类似Instagram的标签,可以在图片中看到。这些标签使用GridView打印在屏幕上。问题是我无法根据GridView项的内容使其宽度动态变化。它获取第一个项目的宽度,并为所有其他项目提供相同的宽度。对于较短的单词,这不是问题,但较长单词的一些字母不可见 下面是问题的屏幕截图 我写的代码C# 如何为gridview项(文本块)指定动态宽度?,c#,xaml,uwp,windows-phone-8.1,windows-10-mobile,C#,Xaml,Uwp,Windows Phone 8.1,Windows 10 Mobile,我正在开发一个Windows10UWP和一个WindowsPhone8.1项目。在该项目中,有类似Instagram的标签,可以在图片中看到。这些标签使用GridView打印在屏幕上。问题是我无法根据GridView项的内容使其宽度动态变化。它获取第一个项目的宽度,并为所有其他项目提供相同的宽度。对于较短的单词,这不是问题,但较长单词的一些字母不可见 下面是问题的屏幕截图 我写的代码 <GridView SelectionMode="None" ItemsSource="{Binding
<GridView SelectionMode="None" ItemsSource="{Binding TagsArray}" ScrollViewer.IsHorizontalRailEnabled="True">
<GridView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock x:Name="tagButton" Tapped="tagButton_Tapped" FontWeight="Medium" Text="{Binding}" Foreground="#063076" FontSize="11" HorizontalAlignment="Left"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="Margin" Value="0,-15,0,-15"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
我建议您为此使用from 您可以通过替换ItemsPanel在GridView中使用它:
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" AllowDrop="True">
</toolkit:WrapPanel>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
下面是一个完整的示例:
<GridView x:Name="ItemGrid" Width="450" HorizontalAlignment="Left" VerticalAlignment="Bottom">
<GridView.ItemTemplate>
<DataTemplate >
<TextBlock Text="{Binding}"/>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<controls:WrapPanel Orientation="Horizontal" AllowDrop="True">
</controls:WrapPanel>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
与以下集合一起使用时:
var ite = new ObservableCollection<string>();
ite.Add("#tag1");
ite.Add("#a");
ite.Add("#tag3");
ite.Add("#differ");
ite.Add("#tag5");
ite.Add("#longertag");
ite.Add("#verylongtag");
ite.Add("#tag1");
this.ItemGrid.ItemsSource = ite;
var ite=新的ObservableCollection();
添加(“#tag1”);
在此处添加(“#a”);
添加(“#tag3”);
添加(“#不同”);
添加(“#tag5”);
添加(“#longertag”);
添加(“#verylongtag”);
添加(“#tag1”);
this.ItemGrid.ItemsSource=ite;
提供以下输出:
我写了自己的WrapGrid,也完成了同样的工作