显示或隐藏特定的TreeView节点

时间:2012-01-02 17:45:50

标签: wpf treeview hierarchicaldatatemplate xmldataprovider

我有以下xml。

<navigation>
  <MenuItems>
<MenuItem>
  <ID>parent_id</ID>
  <title>parent_title</title>
  <name>parent_name</name>
  <desc>parent_desc</desc>
  <styles>
    <properties>
    </properties>
  </styles>
  <submenu>
    <MenuItem>
      <ID>1.1_child_id</ID>
      <title>1.1_child title</title>
      <name>1.1_child name</name>
      <desc>1.1_child desc</desc>
      <styles>
        <properties>
        </properties>
      </styles>
      <submenu>
        <MenuItem>
          <ID>1.1.1_child_id</ID>
          <title>1.1.1_child_title</title>
          <name>1.1.1_child_name</name>
          <desc>1.1.1_child_desc</desc>
          <styles>
            <properties>
            </properties>
          </styles>
        </MenuItem>
        <MenuItem>
          <ID>1.1.2_child_id</ID>
          <title>1.1.2_child_title</title>
          <name>1.1.2_child_name</name>
          <desc>1.1.2_child_desc</desc>
          <styles>
            <properties>
            </properties>
          </styles>
        </MenuItem>
        <MenuItem>
          <ID>1.1.3_child_id</ID>
          <title>1.1.3_child_title</title>
          <name>1.1.3_child_name</name>
          <desc>1.1.3_child_desc</desc>
          <styles>
            <properties>
            </properties>
          </styles>
          <submenu>
            <MenuItem>
              <ID>1.1.2.1_child_id</ID>
              <title>1.1.2.1_child_title</title>
              <name>1.1.2.1_child_name</name>
              <desc>1.1.2.1_child_desc</desc>
              <styles>
                <properties>
                </properties>
              </styles>
            </MenuItem>
            <MenuItem>
              <ID>1.1.2.2_child_id</ID>
              <title>1.1.2.2_child_title</title>
              <name>1.1.2.2_child_name</name>
              <desc>1.1.2.2_child_desc</desc>
              <styles>
                <properties>
                </properties>
              </styles>
            </MenuItem>
          </submenu>
        </MenuItem>
        <MenuItem>
          <ID>1.1.4_child_id</ID>
          <title>1.1.4_child_title</title>
          <name>1.1.4_child_name</name>
          <desc>1.1.4_child_desc</desc>
          <styles>
            <properties>
            </properties>
          </styles>
        </MenuItem>
      </submenu>
    </MenuItem>

使用WPF TreeView我想只显示如下所示的标题。

  • PARENT_TITLE
    • 1.1_child_title
      • 1.1.1_child_title
      • 1.1.2_child_title
      • 1.1.3_child_title
      • 1.1.4_child_title
    • 1.2_child_title

...

以下代码显示了所有内容

<Window x:Class="TreeViewTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <HierarchicalDataTemplate x:Key="NodeTemplate">
         <TextBlock x:Name="text" Text="?" /> 
        <HierarchicalDataTemplate.ItemsSource>
            <Binding XPath="child::node()" />
        </HierarchicalDataTemplate.ItemsSource>
        <HierarchicalDataTemplate.Triggers>
              <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                <Setter TargetName="text" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>

            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="text" Property="Text" Value="{Binding Path=Name}"></Setter>
                <Setter TargetName="text" Property="Foreground" Value="Blue"></Setter>
            </DataTrigger>

        </HierarchicalDataTemplate.Triggers>


    </HierarchicalDataTemplate>



    <XmlDataProvider x:Key="xmlDataProvider" Source="K:\chary\programs\nav.xml"></XmlDataProvider>
</Window.Resources>

<Grid >
    <TreeView Name="treeView1" 
          Background="AliceBlue" 
          ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}" 
          ItemTemplate= "{StaticResource NodeTemplate}"/>
</Grid>

用Google搜索但没有运气。

如何实现这一目标。

0 个答案:

没有答案