예제 #1
0
        /// <summary>
        /// 删除数据对应的节点,若节点为选择状态,则删除后选择同层后一节点或父节点
        /// </summary>
        /// <param name="p_item"></param>
        public void DeleteItem(object p_item)
        {
            var item = (from row in RootItems.GetAllItems()
                        where row.Data == p_item
                        select row).FirstOrDefault();

            if (item == null)
            {
                return;
            }

            int index;

            if (item.Parent == null)
            {
                index = RootItems.IndexOf(item);
                if (index > -1)
                {
                    RootItems.RemoveAt(index);
                    if ((bool)item.IsSelected)
                    {
                        if (index < RootItems.Count)
                        {
                            SelectedItem = RootItems[index].Data;
                        }
                        else if (RootItems.Count > 0)
                        {
                            SelectedItem = RootItems[RootItems.Count - 1].Data;
                        }
                        else
                        {
                            SelectedItem = null;
                        }
                    }
                }
            }
            else
            {
                var ls = item.Parent.Children;
                index = ls.IndexOf(item);
                if (index > -1)
                {
                    ls.RemoveAt(index);
                    if ((bool)item.IsSelected)
                    {
                        if (index < ls.Count)
                        {
                            SelectedItem = ls[index].Data;
                        }
                        else if (ls.Count > 0)
                        {
                            SelectedItem = ls[ls.Count - 1].Data;
                        }
                        else
                        {
                            SelectedItem = item.Parent.Data;
                        }
                    }
                }
            }
            RootItems.Invalidate();
        }