在 Treeview 上使用 ItemContainerStyle 时遇到问题
我在树视图上设置了以下 XML:
<Root Value="YES">
<Child Name="Test">
<Sibling Data="Yes">
<Last UserData="1"/>
</Sibling>
<Sibling Data="No"/>
</Child>
<Child Name="Test2"/>
</Root>
然后在窗口中设置了以下代码:
<Window.Resources>
<XmlDataProvider x:Key="dataProvider" XPath="Root" Source="C:\XML.xml" />
<HierarchicalDataTemplate DataType="Root" ItemsSource="{Binding XPath=*}">
<TextBlock Text="{Binding XPath=@Value}" />
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Blue">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Child" ItemsSource="{Binding XPath=*}">
<TextBlock Text="{Binding XPath=@Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Sibling" ItemsSource="{Binding XPath=*}">
<TextBlock Text="{Binding XPath=@Data}" />
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Margin="12" x:Name="trv"
ItemsSource="{Binding Source={StaticResource dataProvider}}" />
</Grid>
我希望有一个边框控件环绕每个节点的所有子项,如下图所示:
http://www.hardcodet.net/uploads/2008/03/tree-dialogik.png< /a>
换句话说,您会注意到在我链接到的图像中,父节点 dialogik.Memory 有一个深灰色边框围绕着它及其子节点。这就是我想要达到的效果。
我需要在代码中更改哪些内容才能使其正常工作???
谢谢 !!
I have the following XML being set on my treeview:
<Root Value="YES">
<Child Name="Test">
<Sibling Data="Yes">
<Last UserData="1"/>
</Sibling>
<Sibling Data="No"/>
</Child>
<Child Name="Test2"/>
</Root>
and then I have set the following code in my window:
<Window.Resources>
<XmlDataProvider x:Key="dataProvider" XPath="Root" Source="C:\XML.xml" />
<HierarchicalDataTemplate DataType="Root" ItemsSource="{Binding XPath=*}">
<TextBlock Text="{Binding XPath=@Value}" />
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Blue">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Child" ItemsSource="{Binding XPath=*}">
<TextBlock Text="{Binding XPath=@Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Sibling" ItemsSource="{Binding XPath=*}">
<TextBlock Text="{Binding XPath=@Data}" />
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Margin="12" x:Name="trv"
ItemsSource="{Binding Source={StaticResource dataProvider}}" />
</Grid>
I would like to have a border control wrap around all the subitems for every node as in this image:
http://www.hardcodet.net/uploads/2008/03/tree-dialogik.png
In other words, you'll notice in the image I've linked to, the parent node dialogik.Memory has a dark gray border that goes around it and around its children. That's the effect I want to achieve.
What do I need to change in my code to have it work correctly ???
Thanks !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对于 TreeView 来说是不可能的。不过,您可以使用 Expander,请参阅此处:
..然后以类似方式绑定它:您将有一个为每个项目输出一个 Expander 的 ItemsControl,然后 Expander 将包含另一个用于子项目的 ItemsControl ,递归地。
希望有帮助!
That's not possible with the TreeView. You could use an Expander though, see here:
..and then bind it similarly: you'd have an ItemsControl that outputs an Expander for each item, and the Expander would then contain another ItemsControl for child items, recursively.
Hope that helps!