在treeview xaml文件下面:
<TreeView Name="templatetree"
SelectedItemChanged="templatetree_SelectedItemChanged"
MaxHeight="210" Background="{StaticResource MediumGrayBrush}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="IsSelected" Value="False" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="#FF1E90FF"/>
<HierarchicalDataTemplate DataType="{x:Type Tree:Templates}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<Image Source="../Images/template.png"
Height="22" Width="22" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Name}"
Style="{StaticResource PopupBodyTextStyle}"
FontFamily="{StaticResource Symbol}" MouseLeftButtonDown="HeaderNode_MouseLeftButtonDown"/>
<TextBlock Text=" [" Foreground="{StaticResource HighlightBrush}"/>
<TextBlock Text="{Binding Members.Count}" Foreground="{StaticResource HighlightBrush}"/>
<TextBlock Text="]" Foreground="{StaticResource HighlightBrush}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type Tree:TemplateTypes}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Style="{StaticResource PopupBodyTextStyle}"
FontFamily="{StaticResource Symbol}"
MouseLeftButtonDown="MemberNode_MouseLeftButtonDown"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
类文件如下:
public class Templates
{
public string Name { get; set; }
public ObservableCollection<TemplateTypes> Members { get; set; } = new ObservableCollection<TemplateTypes>();
}
public class TemplateTypes
{
public string Name { get; set; }
public string Version { get; set; }
}
输出如:
Template1
--Template1.1
--Template1.2
Template2
--Template2.1
我希望通过编程方式为子节点(Template1.1)设置IsSelected
TreeViewItem
属性,但它只为父节点(Template1)提供了
我试图从treeview获取项目,但它仅提供父节点
{
.
.
.
.
templatetree.ItemsSource = nodeList;
}
TreeViewItem tr = (TreeViewItem)templatetree.ItemContainerGenerator.ContainerFromItem(templatetree.Items[0]);\\This lines gives me parent node of Template Type.
如何为子节点设置IsSelected
属性?
预期结果:Child item selection
答案 0 :(得分:0)
要为第二级添加样式,您需要在HierarchicalDataTemplate中设置它,如下所示
<HierarchicalDataTemplate DataType="{x:Type Tree:Templates}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<Image Source="../Images/template.png"
Height="22" Width="22" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Name}"
Style="{StaticResource PopupBodyTextStyle}"
FontFamily="{StaticResource Symbol}" MouseLeftButtonDown="HeaderNode_MouseLeftButtonDown"/>
<TextBlock Text=" [" Foreground="{StaticResource HighlightBrush}"/>
<TextBlock Text="{Binding Members.Count}" Foreground="{StaticResource HighlightBrush}"/>
<TextBlock Text="]" Foreground="{StaticResource HighlightBrush}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="False" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
下面是样式多级TreeViewItem
的示例 <DataTemplate x:Key="MenuItemTemplate" >
<TreeViewItem/>
</DataTemplate>
<HierarchicalDataTemplate x:Key="ThirdLevel" ItemTemplate="{StaticResource MenuItemTemplate}">
<TextBlock Text="Text" />
<HierarchicalDataTemplate.ItemContainerStyle>
<Style>
<!-- 3rd STYLE HERE -->
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="SecondLevel" ItemTemplate="{StaticResource ThirdLevel}">
<TextBlock Text="Text" />
<HierarchicalDataTemplate.ItemContainerStyle>
<Style>
<!-- 2nd STYLE HERE -->
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
<TreeView ItemTemplate="{StaticResource SecondLevel}">
<TreeView.ItemContainerStyle>
<Style>
<!-- 1st Level Style -->
</Style>
</TreeView.ItemContainerStyle>
</TreeView>