ControlTemplate用于TreeView的第二级

时间:2009-10-18 13:58:50

标签: wpf treeview controltemplate hierarchicaldatatemplate

我正在尝试创建一个TreeView,它有两个级别,但我无处可去。我有ItemTemplateSelector(有效 - 选择下面列出的前两个模板中的任何一个),但我无法使用DataTemplate填充子项。这是我的代码:

public class PlannedMealGroup : INotifyPropertyChanged {
        public ObservableCollection<PlannedMeal> Meals;
        public Constants.MealTimes MealTime;
  ...// these implement INotifyPropertyChanged
}

<HierarchicalDataTemplate x:Key="groupTemplate" ItemsSource="{Binding Meals}">
    <TextBlock Text="{Binding Path=MealTime}"/>
    <HierarchicalDataTemplate.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="tre_PreviewMouseRightButtonDown"/>
        </Style>
    </HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
<DataTemplate x:Key="mealTemplate">
    <Border Name="Border" Margin="4,2" Padding="3" Background="Transparent" IsHitTestVisible="True" SnapsToDevicePixels="true" BorderThickness="0.6" CornerRadius="3">
        <Border Name="InnerBorder" Background="Transparent" IsHitTestVisible="True">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Recipe.Icon,Converter={StaticResource IconConverter}, ConverterParameter=16}" Stretch="None" />
                <TextBlock Text="{Binding Converter={StaticResource RecipeServingConverter}}"/>
            </StackPanel>
        </Border>
    </Border>

<TreeView ItemTemplateSelector="{StaticResource PlannedMealTemplateSelector}" Style="{StaticResource GroupedTreeView}">
  <TreeView.ItemsPanel>
      <ItemsPanelTemplate>
          <StackPanel IsItemsHost="True" Orientation="{Binding Orientation,RelativeSource={x:Static RelativeSource.TemplatedParent}}" />
      </ItemsPanelTemplate>
  </TreeView.ItemsPanel>
  <TreeView.ContextMenu>
  ...

<Style x:Key="GroupedTreeViewItem" TargetType="{x:Type TreeViewItem}">
  <Setter Property="IsExpanded" Value="True"/>
  <Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type TreeViewItem}">
    <Border Name="Border" Margin="4,2" Padding="3" Background="{StaticResource GroupBackgroundBrush}" IsHitTestVisible="True" SnapsToDevicePixels="true" BorderThickness="0.6" CornerRadius="3">
            <Expander Name="Exp" IsExpanded="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}},Mode=TwoWay}">
                <Expander.Header>
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                        x:Name="PART_Header" ContentSource="Header"/>
                </Expander.Header>
                <ItemsPresenter x:Name="ItemsHost"/>
            </Expander>
        </Border>
    </ControlTemplate>
  </Setter.Value>
  </Setter>
</Style>
<Style x:Key="GroupedTreeView" TargetType="TreeView">
    <Setter Property="ItemContainerStyle" Value="{StaticResource GroupedTreeViewItem}"/>
</Style>

感谢您的任何指示。

1 个答案:

答案 0 :(得分:3)

这个问题困扰了我好几个小时,所以我会把它发给别人。我需要为第一个模板资源中的第二级项目设置样式。

<HierarchicalDataTemplate x:Key="groupTemplate" ItemsSource="{Binding Meals}">
            <TextBlock Text="{Binding Path=MealTime}"/>
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="tre_PreviewMouseRightButtonDown"/>
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                    x:Name="PART_Header" ContentSource="Header"/>
...