在 WPF Listbox
,我对这两个概念感到困惑:ItemTemplate
和 ItemsPanelTemplate
有人可以解释我更多吗?
谢谢
约翰
最佳答案
让我试着通过例子来解释这一点:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="SteelBlue" Padding="5" BorderBrush="Black"
BorderThickness="1" Margin="0,2">
<TextBlock Text="{Binding}"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="DarkKhaki"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
结果:
ItemTemplate
确定列表中每个项目的布局。另一方面ItemsPanel
是包含单个项目的面板。鉴于上述定义,可视化树将类似于以下内容:<StackPanel>
<Border>
<TextBlock Text="Alpha"/>
</Border>
<Border>
<TextBlock Text="Beta"/>
</Border>
....
</StackPanel>
关于wpf - ItemTemplate 和 ItemPanelTemplate 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1409514/