wpf - ItemTemplate 和 ItemPanelTemplate 有什么区别?

标签 wpf itemtemplate itemspaneltemplate

在 WPF Listbox ,我对这两个概念感到困惑:ItemTemplateItemsPanelTemplate有人可以解释我更多吗?

谢谢
约翰

最佳答案

让我试着通过例子来解释这一点:

<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>

结果:

WPF ListBox templates example
ItemTemplate确定列表中每个项目的布局。另一方面ItemsPanel是包含单个项目的面板。鉴于上述定义,可视化树将类似于以下内容:
<StackPanel>
  <Border>
    <TextBlock Text="Alpha"/>
  </Border>
  <Border>
    <TextBlock Text="Beta"/>
  </Border>
  ....
</StackPanel>

关于wpf - ItemTemplate 和 ItemPanelTemplate 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1409514/

相关文章:

wpf - ItemsControl 在 MainWindow 的构造函数中没有子项

wpf - 单击、拖动和滚动 Canvas View

c# - 更改项目的显示方式 WPF 列表框

c# - 仅在 wpf 上显示 X 和最小化按钮

asp.net - 在 ASP.Net 数据绑定(bind)表达式中设置日期格式

c# - 中继器 ItemTemplate 中的 If 语句

c# - 将 DataContext 窗口绑定(bind)到 ViewModel

wpf - 在WPF中以编程方式显示菜单

wpf - 在 WPF 中开发接口(interface)的基础知识方面需要帮助