获取根节点索引

时间:2019-07-11 02:56:48

标签: c# uwp treeview

this example中,索引为0。我的代码有多个根。如何获取所选根目录的索引?

 private void TreeView_DragItemsCompleted(TreeView sender, TreeViewDragItemsCompletedEventArgs args)
 {
            var children = sourceTreeView.RootNodes[0].Children;

            if (deletedItem != null)
            {
                for (int i = 0; i < children.Count; i++)
                {
                    if (children[i].Content.ToString() == deletedItem.Content.ToString())
                    {
                        children.RemoveAt(i);
                        break;
                    }
                }
            }

            sourceTreeView = null;
            deletedItem = null;
 }

1 个答案:

答案 0 :(得分:0)

  

获取RootNode的索引

请参考这种情况reply。您可以创建诸如案例回复之类的可绑定数据源。并为IsSelected绑定TreeViewItem属性。当选定的项目IsSelected的值将被更改时,您可以为每个项目来源输入然后获取选定项目的索引。

<TreeViewItem 
    AutomationProperties.Name="{x:Bind Name}"
    IsExpanded="{x:Bind IsExpanded, Mode=TwoWay}"
    IsSelected="{x:Bind IsSelected, Mode=TwoWay}"
    ItemsSource="{x:Bind Children}"
    >
    <StackPanel Orientation="Horizontal">
        <Image Width="20" Source="../Assets/folder.png" />
        <TextBlock Margin="0,0,10,0" />
        <TextBlock Text="{x:Bind Name}" />
    </StackPanel>
</TreeViewItem>

获取索引

 private int GetIndex(ObservableCollection<ExplorerItem> DataSource)
 {
     int index = 0;
     foreach (var item in DataSource)
     {
         if (item.IsSelected == true)
         {
             index = DataSource.IndexOf(item);                 
         }
     }
     return index;
 }