/// <summary> /// 选择行集合变化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnSelectedItemsChanged(object sender, NotifyCollectionChangedEventArgs e) { // HasSelected状态 bool hasSelected = HasSelected; if (_selectedRows.Count == 0 && hasSelected) { ClearValue(HasSelectedProperty); } else if (_selectedRows.Count > 0 && !hasSelected) { HasSelected = true; } if (e.Action == NotifyCollectionChangedAction.Add) { // 增加 if (e.NewItems != null && e.NewItems.Count > 0) { List <object> adds = new List <object>(); foreach (var row in e.NewItems.OfType <TvItem>()) { row.IsSelected = true; adds.Add(row.Data); } SelectionChanged?.Invoke(this, new SelectionChangedEventArgs(new List <object>(), adds)); } } else if (e.Action == NotifyCollectionChangedAction.Remove) { // 删除 if (e.OldItems != null && e.OldItems.Count > 0) { List <object> removals = new List <object>(); foreach (var row in e.OldItems.OfType <TvItem>()) { row.IsSelected = false; removals.Add(row.Data); } SelectionChanged?.Invoke(this, new SelectionChangedEventArgs(removals, new List <object>())); } } else if (e.Action == NotifyCollectionChangedAction.Reset) { // 清空 var removals = new List <object>(); foreach (var row in RootItems.GetAllItems()) { if (row.IsSelected.HasValue && row.IsSelected.Value) { removals.Add(row.Data); } row.IsSelected = false; } if (removals.Count > 0 && SelectionChanged != null) { SelectionChanged(this, new SelectionChangedEventArgs(removals, new List <object>())); } } }
/// <summary> /// 获取同层下面的节点 /// </summary> /// <param name="p_item"></param> /// <returns></returns> public object GetFollowingBrother(object p_item) { var item = (from row in RootItems.GetAllItems() where row.Data == p_item select row).FirstOrDefault(); if (item == null) { return(null); } int index; if (item.Parent == null) { index = RootItems.IndexOf(item); if (index >= 0 && index < RootItems.Count - 1) { return(RootItems[index + 1].Data); } } else { index = item.Parent.Children.IndexOf(item); if (index >= 0 && index < item.Parent.Children.Count - 1) { return(item.Parent.Children[index + 1].Data); } } return(null); }
void SetAllExpandState(bool p_isExpanded) { foreach (var item in RootItems.GetAllItems()) { item.SetExpandState(p_isExpanded); } RootItems.Invalidate(); }
/// <summary> /// 滚动到指定的节点 /// </summary> /// <param name="p_item"></param> public void ScrollInto(object p_item) { if (_panel != null) { var item = (from row in RootItems.GetAllItems() where row.Data == p_item select row).FirstOrDefault(); if (item != null) { _panel.ScrollIntoItem(item); } } }
/// <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(); }