【发布时间】:2021-03-12 17:06:16
【问题描述】:
我有一个包含父母和几个孩子的 TreeView。孩子依次由一个带有自己的孩子(“一”、“二”、“三”等)的 WrapPanel 组成。当父窗口不足以容纳它们时,如何让这些最后的元素换行?
这是我的代码:
<TreeView ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<TreeViewItem Header="Parent" IsExpanded="True" >
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Items>
<TextBlock Text="One" />
<TextBlock Text="Two" />
<TextBlock Text="Three" />
<TextBlock Text="Four" />
<TextBlock Text="Five" />
<TextBlock Text="Six" />
<TextBlock Text="Seven" />
<TextBlock Text="Eight" />
<TextBlock Text="Nine" />
<TextBlock Text="Ten" />
</ItemsControl.Items>
</ItemsControl>
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Items>
<TextBlock Text="One" />
<TextBlock Text="Two" />
<TextBlock Text="Three" />
<TextBlock Text="Four" />
<TextBlock Text="Five" />
<TextBlock Text="Six" />
<TextBlock Text="Seven" />
<TextBlock Text="Eight" />
<TextBlock Text="Nine" />
<TextBlock Text="Ten" />
</ItemsControl.Items>
</ItemsControl>
</TreeViewItem>
</TreeView>
这是它目前产生的结果:
【问题讨论】:
-
通过将宽度设置为ItemsControl/WrapPanel来实现换行