我有一个奇怪的WPF/XAML问题。默认情况下,我希望展开此TreeView中的所有节点。最终,我会将其绑定到我的视图模型,但现在我只想在默认情况下将它们全部展开。
这就是有问题的代码部分(我目前正在使用)
<HierarchicalDataTemplate DataType="{x:Type Model:DirectoryItem}"
ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}"
ToolTip="{Binding Path=Path}" />
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type Model:FileItem}">
<TextBlock Text="{Binding Path=Name}"
ToolTip="{Binding Path=Path}" />
</DataTemplate>特别是,我将setter属性设置为IsExpanded的部分。
正如你从下面的图片中看到的,这段代码可以说是正常工作的。如果我展开根节点,那么默认情况下所有内容都会展开。
但是为什么根节点在默认情况下也没有展开呢?我不知道它为什么要这样做。


发布于 2012-03-31 20:35:40
它们没有展开,可能是因为您正在将样式添加到分层数据模板中,而这只适用于子代(尽管只适用于理论)。
在DataTemplate中使用样式是一种代码味道--它是数据的模板,而不是可视化的表示,所以它不应该包含treeViewItem的样式(下一次可能是其他的东西,也是分层的)。
我假设你是这样做的:
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>https://stackoverflow.com/questions/9955677
复制相似问题