gpt4 book ai didi

wpf - TreeView 第二层的 ControlTemplate

转载 作者:行者123 更新时间:2023-12-02 11:51:53 25 4
gpt4 key购买 nike

我正在尝试创建一个具有两个级别的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>

感谢您的指点。

最佳答案

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

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

关于wpf - TreeView 第二层的 ControlTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1584959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com