如何水平使用 ListView 来显示不同宽度的项目?
How can I use a ListView horizontally to show items of varying width?
我有一个 ListView,其 ItemsSource 是一个字符串集合。正如您所料,字符串的集合具有不同的长度。我的目标是在 listView 中水平显示这些字符串。
有很多水平使用 ListView 的示例,所以这部分很简单。我通过将 Horizontal StackPanel 指定为 ItemsPanelTemplate 来实现这一点...
我的列表视图xaml:
<ListView x:Name="myListView">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Click="Button_Click" FontSize="10" Foreground="Salmon" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="x" Background="Transparent" />
<Label FontSize="10" Foreground="SteelBlue" Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
我要显示的内容:
[This is a string] [This is also a string] [abc] [Look at me]
我的 xaml 显示的内容:
[This i] [This i] [abc ] [Look a]
我可以为我的 GridviewColumn 设置一个宽度,然后得到如下内容:
[This is a string ][This is also a string] [abc ][Look at me ]
我还可以将列宽设置为自动,这将使其与最宽的项目一样大...但我真的很希望它能够保留 space 并将它们并排放置在它们的真实位置宽度。
我做了很多研究都没有成功,有什么想法吗?
我不确定如何在 ListView 中实现这一点。但我试过使用 ListBox 效果很好。参考下面的代码。
<ListBox x:Name="myListView">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Click="Button_Click" FontSize="10" Foreground="Salmon"
BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="x" Background="Transparent" />
<Label FontSize="10" Foreground="SteelBlue" Width="Auto" Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我有一个 ListView,其 ItemsSource 是一个字符串集合。正如您所料,字符串的集合具有不同的长度。我的目标是在 listView 中水平显示这些字符串。
有很多水平使用 ListView 的示例,所以这部分很简单。我通过将 Horizontal StackPanel 指定为 ItemsPanelTemplate 来实现这一点...
我的列表视图xaml:
<ListView x:Name="myListView">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Click="Button_Click" FontSize="10" Foreground="Salmon" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="x" Background="Transparent" />
<Label FontSize="10" Foreground="SteelBlue" Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
我要显示的内容:
[This is a string] [This is also a string] [abc] [Look at me]
我的 xaml 显示的内容:
[This i] [This i] [abc ] [Look a]
我可以为我的 GridviewColumn 设置一个宽度,然后得到如下内容:
[This is a string ][This is also a string] [abc ][Look at me ]
我还可以将列宽设置为自动,这将使其与最宽的项目一样大...但我真的很希望它能够保留 space 并将它们并排放置在它们的真实位置宽度。
我做了很多研究都没有成功,有什么想法吗?
我不确定如何在 ListView 中实现这一点。但我试过使用 ListBox 效果很好。参考下面的代码。
<ListBox x:Name="myListView">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Click="Button_Click" FontSize="10" Foreground="Salmon"
BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="x" Background="Transparent" />
<Label FontSize="10" Foreground="SteelBlue" Width="Auto" Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>