2011-04-04 77 views
0

我知道一個ControlTemplate是假設完全替換默認的,這似乎在不使用HierarchicalDataTemplate的時候工作。然而,使用HierarchicalDataTemplate我的控制模板似乎部分使用 - TreeViewItem是我指定的控件包含圖像等,但仍然顯示爲具有默認擴展器以顯示其子的節點 - 未在我的模板中指定(我希望我的孩子總是被顯示出來,但那在旁邊)。它看起來像這樣:WPF TreeViewItem控件模板部分應用?

 <TreeView> 
     <TreeView.Resources> 

      <Style x:Key="MyNodeStyle" TargetType="TreeViewItem"> 
       <Setter Property="SnapsToDevicePixels" Value="true" /> 
       <Setter Property="OverridesDefaultStyle" Value="true"/> 
       <Setter Property="IsExpanded" Value="true"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="TreeViewItem"> 
          <StackPanel Orientation="Vertical" HorizontalAlignment="Center"> 
           <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" BorderBrush="{StaticResource itemBorderBrush}" Background="{StaticResource itemBackgroundBrush}" x:Name="border"> 
            <DockPanel LastChildFill="False"> 
             <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80"> 
              <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/> 
              <Image Source="MyNode.png" Stretch="None"/> 
              <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/>             
             </StackPanel> 
            </DockPanel> 
           </Border> 
           <ItemsPresenter /> 
          </StackPanel> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="ItemsPanel"> 
        <Setter.Value> 
         <ItemsPanelTemplate> 
          <StackPanel Orientation="Horizontal" IsItemsHost="True" HorizontalAlignment="Center" /> 
         </ItemsPanelTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 

      <HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" > 
       <TreeViewItem Style="{StaticResource MyNodeStyle}"/> 
      </HierarchicalDataTemplate> 

     </TreeView.Resources> 

     <!-- Arrange the root items horizontally. --> 
     <TreeView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel IsItemsHost="True" Orientation="Horizontal" HorizontalAlignment="Center" /> 
      </ItemsPanelTemplate> 
     </TreeView.ItemsPanel> 

    </TreeView> 

對於使用HierarchicalDataTemplate的ItemsPanel只有當和Setter似乎並沒有被應用,孩子們在默認的擴展提出了一些原因。當我使用我自己的ControlTemplate時,擴展器是如何到達的?@#$

回答

1

我不認爲你應該把TreeViewItem放在你的HierarchicalDataTemplate之內。

試試這個:

<HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" > 
    <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80"> 
     <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/> 
     <Image Source="MyNode.png" Stretch="None"/> 
     <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/> 
    </StackPanel> 
</HierarchicalDataTemplate> 

現在,你的模板就變成了:

<ControlTemplate TargetType="TreeViewItem"> 
    <StackPanel Orientation="Vertical" HorizontalAlignment="Center"> 
     <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" x:Name="border"> 
      <DockPanel LastChildFill="False"> 
       <ContentPresenter ContentSource="Header" /> 
      </DockPanel> 
     </Border> 
     <ItemsPresenter /> 
    </StackPanel> 
</ControlTemplate> 

是你打算如何就看?

編輯:原來的擴展可能是因爲有你只用你的風格的子項 - 讓你的風格ItemContainerStyle爲樹形:

<Window.Resources> 
    <Style x:Key="MyNodeStyle" TargetType="TreeViewItem"> 
     .... 

<TreeView ItemContainerStyle="{StaticResource MyNodeStyle}">