在 Treeview 上使用 ItemContainerStyle 时遇到问题

发布于 2024-09-08 08:16:09 字数 2038 浏览 4 评论 0原文

我在树视图上设置了以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2024-09-15 08:16:09

这对于 TreeView 来说是不可能的。不过,您可以使用 Expander,请参阅此处:

http://msdn.microsoft.com /en-us/library/system.windows.controls.expander.aspx

..然后以类似方式绑定它:您将有一个为每个项目输出一个 Expander 的 ItemsControl,然后 Expander 将包含另一个用于子项目的 ItemsControl ,递归地。

希望有帮助!

That's not possible with the TreeView. You could use an Expander though, see here:

http://msdn.microsoft.com/en-us/library/system.windows.controls.expander.aspx

..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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文