要实现的效果是用Wrappanel作为ListBoxItem的容器。
有两种方法可以实现该效果。
方法一:使用ItemsControl.ItemsPanel。
参考代码如下:
<
ListBox.ItemsPanel
>
< ItemsPanelTemplate >
< WrapPanel Orientation ="Horizontal" IsItemsHost ="True" ></ WrapPanel >
</ ItemsPanelTemplate >
</ ListBox.ItemsPanel >
< ItemsPanelTemplate >
< WrapPanel Orientation ="Horizontal" IsItemsHost ="True" ></ WrapPanel >
</ ItemsPanelTemplate >
</ ListBox.ItemsPanel >
方法二:使用ControlTemplate
参考代码如下:
<
ListBox.Template
>
< ControlTemplate TargetType ="ListBox" >
< WrapPanel Orientation ="Horizontal" IsItemsHost ="True" />
</ ControlTemplate >
</ ListBox.Template >
< ControlTemplate TargetType ="ListBox" >
< WrapPanel Orientation ="Horizontal" IsItemsHost ="True" />
</ ControlTemplate >
</ ListBox.Template >
最后的结果是方法一无法实现效果,方法二可以实现效果,但是还需要定义一些界面效果。