更改 TreeView ItemsPanel 方向无效 [英] Changing the TreeView ItemsPanel orientation has no effect
问题描述
TreeView
的默认 ItemsPanel
是具有默认方向 Vertical
的 StackPanel
.我已将 StackPanel
的方向更改为 Horizontal
:
The default ItemsPanel
of a TreeView
is a StackPanel
that has the default orientation Vertical
. I have changed the orientation of the StackPanel
to Horizontal
:
<controls:TreeView>
<controls:TreeView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</controls:TreeView.ItemsPanel>
<controls:TreeViewItem Header="Root" IsExpanded="True">
<controls:TreeViewItem Header="Alfa"/>
<controls:TreeViewItem Header="Beta"/>
<controls:TreeViewItem Header="Gamma"/>
</controls:TreeViewItem>
</controls:TreeView>
我希望树中的下级项目水平堆叠而不是垂直堆叠,但是当我将方向从 Vertical
交换到 Horizontal
并返回时,我看不到任何明显变化再次.为什么StackPanel
的Orientation
不影响ItemsPresenter
中项目的布局?
I would expect the subordinate items in the tree to stack horizontally instead of vertically, but I see no visible change when I swap the orientation from Vertical
to Horizontal
and back again. Why doesn't the Orientation
of the StackPanel
affect the layout of the items in the ItemsPresenter
?
推荐答案
Jeff Wilcox 的评论让我思考并找出我做错了什么.我正在修改 TreeView
的 ItemsPanel
,但是为了达到我想要的效果,我必须修改 TreeViewItem
的 ItemsPanel
代码>:
Jeff Wilcox' comment got me thinking and I figured out what I did wrong. I was modifying the ItemsPanel
of the TreeView
, but to achieve what I wanted I had to modify the ItemsPanel
of the TreeViewItem
:
<controls:TreeView>
<controls:TreeView.ItemContainerStyle>
<Style TargetType="controls:TreeViewItem">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</controls:TreeView.ItemContainerStyle>
<controls:TreeViewItem Header="Root" IsExpanded="True">
<controls:TreeViewItem Header="Alfa"/>
<controls:TreeViewItem Header="Beta"/>
<controls:TreeViewItem Header="Gamma"/>
</controls:TreeViewItem>
</controls:TreeView>
这篇关于更改 TreeView ItemsPanel 方向无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!