【问题标题】:ControlTemplate for second level of TreeView用于 TreeView 第二级的 ControlTemplate
【发布时间】:2009-10-18 13:58:49
【问题描述】:

我正在尝试创建一个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>

感谢任何指点。

【问题讨论】:

    标签: wpf treeview controltemplate hierarchicaldatatemplate


    【解决方案1】:

    这个问题困扰了我几个小时,所以我会为其他人发布此问题。我需要在第一个模板的资源中设置二级项目的样式。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2016-11-14
      • 2022-10-16
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      相关资源
      最近更新 更多