我想水平排列一些TextBlocks,没有任何边距。我可以看到我的TextBlock尽可能小,但由于某些原因,它周围增加了很多空间。我认为它与ListView或其样式有关,但我不知道是什么。
我有以下布局:
<ListView Width="Auto" SelectionMode="None" Background="#654321" ItemsSource="{Binding}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="Black">
<TextBlock Text="{Binding}" Margin="0,0,0,0"/>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<x:String>A</x:String>
<x:String>B</x:String>
<x:String>C</x:String>
<x:String>D</x:String>
<x:String>E</x:String>
</ListView>
答案 0 :(得分:4)
您需要更改的是ItemContainerStyle
。您可以从here获取Style
。
值得注意的是Margin
的默认Style
是:
<Setter Property="Margin" Value="0,0,18,2"/>
这解释了巨大的差距。将其更改为:
<Setter Property="Margin" Value="0,0,0,2"/>
这应该有助于解决您的问题。
最后要注意的是,他们还有一个默认的ContentMargin="4"
(如果使用ListViewItemsPresenter
)或一堆Margin
的4遍在整个风格中(特别是在Border
s),您可能也需要更改。
答案 1 :(得分:3)
另一种查看方式 - ListView
实际上是设计为控件,您可以点按,选择,拖动和以其他方式与项目交互。我想如果你想删除确保尺寸可以用胖手指触摸的所有功能 - 你可能最好使用可能没有这些限制的基础ItemsControl
或ListBox
。如果你摆脱了所有的边距并使你的项目变得如此之小 - 那么使用ListView
毫无意义,这只会使事情变得复杂并使性能变差。