【问题标题】:Having trouble with ItemContainerStyle on a Treeview在 Treeview 上遇到 ItemContainerStyle 问题
【发布时间】:2010-07-06 13:57:39
【问题描述】:

我在树视图上设置了以下 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

换句话说,您会在我链接到的图像中注意到,父节点 dialogik.Memory 有一个深灰色边框,围绕它及其子节点。这就是我想要达到的效果。

我需要对我的代码进行哪些更改才能使其正常工作???

谢谢!!

【问题讨论】:

标签: wpf treeview itemcontainerstyle


【解决方案1】:

TreeView 无法做到这一点。不过,您可以使用 Expander,请参见此处:

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

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

希望有帮助!

【讨论】:

  • 我真的希望你在 3 年后得到这个评论。我有与 OP 完全相同的问题,但我不知道如何实施您的建议。我需要为某些节点在所有子节点周围设置一个边框。你能改变你的答案来证明这一点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 2021-03-17
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多