2011-04-04 75 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}">