开发者

ControlTemplate for second level of TreeView

I'm trying to create a TreeView which has two levels but I'm getting nowhere. I have an ItemTemplateSelector (which works - selecting either of the first two templates listed below), but I can't get the children to populate using a DataTemplate. This is my code:

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,Relat开发者_如何转开发iveSource={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>

Thanks for any pointers.


This problem illuded me for hours, so I'll post this for anyone else. I needed to set the Style for the second level items in the first template's resources.

<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"/>
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜