views:

477

answers:

1

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>

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?

+1  A: 

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