none
TreeView中,无法以TreeviewItem的方式取得子树 RRS feed

  • 问题

  • 我的需求是,将Treeview中指定的(以一个Entity为目标)TreeviewItem选中

    这是我的Treeview的模板

    <DataTemplate x:Key="SubCategoryTree">
                    <TextBlock  Text="{Binding Path=SubCatDesc}" Tag="{Binding DocumentsList}" GotFocus="SubCategoeyGotFocus" />
                </DataTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding SubCategoryList}"  ItemTemplate="{StaticResource SubCategoryTree}"
                    x:Key="CategoryTree" >
                    <TextBlock Text="{Binding Description}" Tag="{Binding DocCatID}"></TextBlock>
                </HierarchicalDataTemplate>

    Treeview,基本上没什么特别的

    <TreeView x:Name="TvCategory" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"  ItemsSource="{Binding}" ItemTemplate="{StaticResource CategoryTree}" />

    CategoryTree的格式是三层的,Category -N: SubCategroy -N: DocumentIndexs,Treeview里面我只需要用到前两层

    这是我寻找所需子树的代码

    for (int i = 0; i < TvCategory.Items.Count; i++)
                    {
                        for (int j = 0; j < ((DocCategory)TvCategory.Items[i]).SubCategoryList.Count; j++)
                        {
                            if (((DocSubCategory)(((DocCategory)TvCategory.Items[i]).SubCategoryList[j])).SubCatDesc == selection.ToString())
                            {

                                TreeViewItem treeMainItem = TvCategory.ItemContainerGenerator.ContainerFromItem(TvCategory.Items[i]) as TreeViewItem;
                                treeMainItem.IsExpanded = true;

                                
                                TreeViewItem treeItem = treeMainItem.ItemContainerGenerator.ContainerFromItem(treeMainItem.Items[j]) as TreeViewItem;
                                treeItem.IsSelected = true;
                                break;
                            }
                        }
                    }

     

    代码走到黑体的部分以后,已经找到了指定的Entity,但就是无法以TreeviewItem的形式打开,treeItem老是为null,请问这是怎么回事呢?

    有没有什么好的解决方式呢

    我的需求是,将Treeview中指定的(以一个Entity为目标)TreeviewItem选中

    2010年6月29日 1:11

全部回复

  • 您好 安道古人,

    您可以尝试一下下面的代码:

      for (int i = 0; i < TvCategory.Items.Count; i++)
      {
        TreeViewItem catItem = TvCategory.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
        if (catItem != null)
        {
          for (int j = 0; j < catItem.Items.Count; j++)
          {
            DocSubCategory subCatItem = catItem.Items[j] as DocSubCategory;
            if (subCatItem != null && subCatItem.SubCatDesc == selection.ToString())
            {
    
              TreeViewItem treeMainItem = catItem.ItemContainerGenerator.ContainerFromIndex(j) as TreeViewItem;
              treeMainItem.IsExpanded = true;
    
              TreeViewItem treeItem = treeMainItem.ItemContainerGenerator.ContainerFromItem(treeMainItem.Items[j]) as TreeViewItem;
              treeItem.IsSelected = true;
              break;
            }
          }
        }
      }

    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    2010年6月30日 8:43
    版主
  • 您好 安道古人,

    您可以尝试一下下面的代码:

     for (int i = 0; i < TvCategory.Items.Count; i++)
    
     {
    
      TreeViewItem catItem = TvCategory.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
    
      if (catItem != null)
    
      {
    
       for (int j = 0; j < catItem.Items.Count; j++)
    
       {
    
        DocSubCategory subCatItem = catItem.Items[j] as DocSubCategory;
    
        if (subCatItem != null && subCatItem.SubCatDesc == selection.ToString())
    
        {
    
    
    
         TreeViewItem treeMainItem = catItem.ItemContainerGenerator.ContainerFromIndex(j) as TreeViewItem;
    
         treeMainItem.IsExpanded = true;
    
    
    
         TreeViewItem treeItem = treeMainItem.ItemContainerGenerator.ContainerFromItem(treeMainItem.Items[j]) as TreeViewItem;
    
         treeItem.IsSelected = true;
    
         break;
    
        }
    
       }
    
      }
    
     }
    
    

    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

    TreeViewItem treeMainItem = catItem.ItemContainerGenerator.ContainerFromIndex(j) as TreeViewItem;

    这一部就取不到了

    如果这样运行:

    for (int i = 0; i < TvCategory.Items.Count; i++)
        {
         TreeViewItem catItem = TvCategory.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
         if (catItem != null)
         {
          for (int j = 0; j < catItem.Items.Count; j++)
          {
           DocSubCategory subCatItem = catItem.Items[j] as DocSubCategory;
           if (subCatItem != null && subCatItem.SubCatDesc == selection.ToString())
           {
    
            //TreeViewItem treeMainItem = catItem.ItemContainerGenerator.ContainerFromIndex(j) as TreeViewItem;
            //treeMainItem.IsExpanded = true;
            catItem.IsExpanded = true;
    
            TreeViewItem treeItem = catItem.ItemContainerGenerator.ContainerFromItem(subCatItem) as TreeViewItem;
            treeItem.IsSelected = true;
            break;
           }
          }
         }
        }
    

    用quickwatch 会发现 catItem.ItemContainerGenerator的Items为null

    而catItem.ItemContainerGenerator的host和peer里面有我想要的TreeviewItem,但这两个是non-Public的

    2010年7月1日 1:21
  • Hi 安道故人,

    这样的话可能要遍历VisualTree了,您可以用XamlPad之类的工具查看一下VisualTree。

    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    2010年7月1日 4:39
    版主