WPF C#Treeview删除&聚焦节点

时间:2016-05-23 03:36:22

标签: c# wpf treeview

我是WPF C#的新手。

我使用Treeview和“HierarchicalDataTemplate”。 首先从源读取数据并将其放入“ObservableCollection()”的简单列表中,然后通过简单递归方法填充Treeview。 这很完美。

另外,我有一个简单的递归搜索方法和Traverse My Treeview。

但遗憾的是我无法删除所选节点和焦点搜索节点。

同样,对于删除选定节点,我通过“MyTreeview.Items.Clear()”执行删除完整树视图节点,并进一步制作Treeview。

为什么当我使用“MyTreeview.Items.Clear()”,“MyTreeview_SelectedItemChanged”时会触发?我怎么能阻止它?

我在本网站和其他网站上搜索,阅读和测试了许多文档,问答和代码,但解决方案建议无法正常工作或者可能在“Windows窗体”中工作。

提前感谢您的帮助。

这是我的代码:

1)XAML代码:

<TreeView Name="tv_Organization" Height="497" HorizontalAlignment="Left" Margin="9,9,0,0" VerticalAlignment="Top" Width="378" 
                   ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                   Loaded="tv_Organization_Loaded" Background="White" SelectedItemChanged="tv_Organization_SelectedItemChanged">

        <TreeView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Add New Node" Click="Organization_Add_TV_Node"/>
                <MenuItem Header="Edit Node" Click="Organization_Edit_TV_Node"/>
                <MenuItem Header="Delete Node" Click="Organization_Delete_TV_Node"/>
            </ContextMenu>
        </TreeView.ContextMenu>

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate  ItemsSource="{Binding NodeItem}">
                    <TextBlock Text="{Binding NodeTitle}" Padding="3,0,3,0" Margin="5,0,0,0"/>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>

    </TreeView>

2)代码背后:

    public class class_Organization
    {
        public class_Organization()
        {
            this.NodeItem = new ObservableCollection<class_Organization>();
        }
        public int ID { get; set; }
        public int Parent_ID { get; set; }
        public long NodeCode { get; set; }
        public string NodeTitle { get; set; }
        public bool IsEnable { get; set; }
        public bool IsLock { get; set; }
        public bool IsDelete { get; set; }
        public bool IsSystem { get; set; }

        public ObservableCollection<class_Organization> NodeItem { get; set; }
    }

    private void tv_Organization_Loaded(object sender, RoutedEventArgs e)
    {
        using (DC DB = new DC())
        {
            lst_OrganizationTree = (from t in DB.OrganizeTrees
                                    orderby t.NodeCode
                                    orderby t.Parent_ID
                                    select new class_Organization()
                                    {
                                        ID = t.ID,
                                        Parent_ID = t.Parent_ID,
                                        NodeCode = t.NodeCode,
                                        NodeTitle = t.NodeTitle,
                                        IsEnable = t.IsEnable,
                                        IsLock = t.IsLock,
                                        IsDelete = t.IsDelete,
                                        IsSystem = t.IsSystem
                                    }).ToList();


            tv_Root = new class_Organization()
            {
                ID = lst_OrganizationTree[0].ID,
                Parent_ID = lst_OrganizationTree[0].Parent_ID,
                NodeCode = lst_OrganizationTree[0].NodeCode,
                NodeTitle = lst_OrganizationTree[0].NodeTitle,
                IsEnable = lst_OrganizationTree[0].IsEnable,
                IsLock = lst_OrganizationTree[0].IsLock,
                IsDelete = lst_OrganizationTree[0].IsDelete,
                IsSystem = lst_OrganizationTree[0].IsSystem
            };

            tv_Organization.Items.Add(tv_Root);
            Recursive_Add_Node(tv_Root);
        }
    }

    private static void Recursive_Add_Node(class_Organization _node)
    {
        foreach (var item in lst_OrganizationTree)
        {
            if (_node.ID == item.Parent_ID)
            {
                class_Organization Childe_Node = new class_Organization()
                {
                    ID = item.ID,
                    Parent_ID = item.Parent_ID,
                    NodeCode = item.NodeCode,
                    NodeTitle = item.NodeTitle,
                    IsEnable = item.IsEnable,
                    IsLock = item.IsLock,
                    IsDelete = item.IsDelete,
                    IsSystem = item.IsSystem
                };

                _node.NodeItem.Add(Childe_Node);
                Recursive_Add_Node(Childe_Node);
            }
        }
    }

    private void tv_Organization_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        UserRowSelect = (class_Organization)tv_Organization.SelectedItem;
        if (UserRowSelect != null)
        {
            Node_ID = UserRowSelect.ID;
        }
    }

    private void Organization_Add_TV_Node(object sender, RoutedEventArgs e)
    {

        // Some of Code for Add new Node to Treeview
    }

    private void Organization_Edit_TV_Node(object sender, RoutedEventArgs e)
    {

        // Some of Code for Edit Last Nodes
    }

    private void Organization_Delete_TV_Node(object sender, RoutedEventArgs e)
    {
        // Some of Code for Delete Last Node

        // I Can't Find  or Cast Parent Node as Treeview Node !!!!!
    }

    private void btn_Accept(object sender, RoutedEventArgs e)
    {
        using (DC DB = new DC())
        {

            try
            {
                var Q = DB.OrganizeTrees.FirstOrDefault(c => c.Parent_ID == Node_ID);
                if (Q == null)
                {
                    var Q1 = DB.OrganizeTrees.FirstOrDefault(c => c.ID == Node_ID);
                    if (Q1 != null)
                    {
                        Q1.IsDelete = true;
                        DB.SubmitChanges();
                    }

                    tv_Organization.Items.Clear();

                    tv_Root = new class_Organization()
                    {
                        ID = lst_OrganizationTree[0].ID,
                        Parent_ID = lst_OrganizationTree[0].Parent_ID,
                        NodeCode = lst_OrganizationTree[0].NodeCode,
                        NodeTitle = lst_OrganizationTree[0].NodeTitle,
                        IsEnable = lst_OrganizationTree[0].IsEnable,
                        IsLock = lst_OrganizationTree[0].IsLock,
                        IsDelete = lst_OrganizationTree[0].IsDelete,
                        IsSystem = lst_OrganizationTree[0].IsSystem
                    };

                    lst_OrganizationTree.RemoveAll((c) => c.NodeCode == UserRowSelect.NodeCode);

                    tv_Organization.Items.Add(tv_Root);
                    Recursive_Add_Node(tv_Root);

                    tv_Organization.Items.Refresh();
                }
                else
                    MessageBox.Show("Error !");

            }
            catch (Exception)
            {
                throw;
            }

        }
    }


    public static void Traverse_Tree(class_Organization _treeview_root)
    {
        foreach (var tn in _treeview_root.NodeItem)
        {
            Traverse_Tree(tn);
            // Use tn Field .....
        }
    }


    public static class_Organization Search_Node(class_Organization _treeview_root, long _nodeCode)
    {
        if (_nodeCode > 0)
        {
            foreach (var tn in _treeview_root.NodeItem)
            {
                if (tn.NodeCode == _nodeCode)
                    return (class_Organization)tn;
            }
        }
        return null;
    }
}

3)输出:(免费多级树视图)

1 个答案:

答案 0 :(得分:0)

删除WPF TreeView所选节点:

yourTreeView.Items.RemoveAt(x); // x is the index of the selected node.