/// <summary> /// 多选模式时级联调整选择状态 /// </summary> /// <param name="p_item"></param> internal void ToggleSelectedCascade(TvItem p_item) { _selectedRows.CollectionChanged -= OnSelectedItemsChanged; var adds = new List <object>(); var removes = new List <object>(); try { if (!p_item.IsSelected.HasValue || p_item.IsSelected.Value) { UnselectCascade(p_item, removes); } else { SelectCascade(p_item, adds); } } finally { _selectedRows.CollectionChanged += OnSelectedItemsChanged; } if (SelectionChanged != null && (adds.Count > 0 || removes.Count > 0)) { SelectionChanged(this, new SelectionChangedEventArgs(removes, adds)); } }
/// <summary> /// 单选模式点击时切换选择 /// </summary> /// <param name="p_item"></param> internal void OnToggleSelected(TvItem p_item) { List <object> removes = new List <object>(); try { _selectedRows.CollectionChanged -= OnSelectedItemsChanged; if (_selectedRows.Count > 0) { var row = _selectedRows[0]; row.IsSelected = false; removes.Add(row.Data); _selectedRows.Clear(); } _selectedRows.Add(p_item); p_item.IsSelected = true; HasSelected = true; } finally { _selectedRows.CollectionChanged += OnSelectedItemsChanged; } SelectionChanged?.Invoke(this, new SelectionChangedEventArgs(removes, new List <object> { p_item.Data })); }
void UpdateParentUnselect(TvItem p_item, List <object> p_data) { bool isAll = true; foreach (var item in p_item.Children) { if (!item.IsSelected.HasValue || item.IsSelected.Value) { isAll = false; break; } } if (isAll) { // 子节点全未选 if (p_item.IsSelected.HasValue && p_item.IsSelected.Value) { _selectedRows.Remove(p_item); p_data.Add(p_item.Data); } p_item.IsSelected = false; } else { // 部分选,未知状态 p_item.IsSelected = null; } p_item = p_item.Parent; while (p_item != null) { UpdateParentUnselect(p_item, p_data); p_item = p_item.Parent; } }
public TvItem(Tv p_tv, object p_data, TvItem p_parent) { _owner = p_tv; Data = p_data; Parent = p_parent; Depth = (p_parent == null) ? 0 : p_parent.Depth + 1; Children = new List <TvItem>(); }
/// <summary> /// 级联选择 /// </summary> /// <param name="p_item"></param> /// <param name="p_data"></param> void SelectCascade(TvItem p_item, List <object> p_data) { p_item.IsSelected = true; _selectedRows.Add(p_item); p_data.Add(p_item.Data); if (p_item.Parent != null) { UpdateParentSelect(p_item.Parent, p_data); } UpdateChildSelect(p_item, p_data); }
/// <summary> /// 展开从根节点到当前节点的所有节点 /// </summary> public void ExpandAll() { TvItem parent = Parent; while (parent != null) { parent.IsExpanded = true; parent = parent.Parent; } if (Children.Count > 0 && !IsExpanded) { IsExpanded = true; } }
void UnselectCascade(TvItem p_item, List <object> p_data) { if (p_item.IsSelected.HasValue && p_item.IsSelected.Value) { _selectedRows.Remove(p_item); p_data.Add(p_item.Data); } p_item.IsSelected = false; if (p_item.Parent != null) { UpdateParentUnselect(p_item.Parent, p_data); } UpdateChildUnselect(p_item, p_data); }
void UpdateChildUnselect(TvItem p_item, List <object> p_data) { if (p_item.Children.Count == 0) { return; } foreach (var item in p_item.Children) { if (item.IsSelected.HasValue && item.IsSelected.Value) { _selectedRows.Remove(p_item); p_data.Add(p_item.Data); } item.IsSelected = false; UpdateChildUnselect(item, p_data); } }
void UpdateChildSelect(TvItem p_item, List <object> p_data) { if (p_item.Children.Count == 0) { return; } foreach (var item in p_item.Children) { if (!item.IsSelected.HasValue || !item.IsSelected.Value) { item.IsSelected = true; _selectedRows.Add(p_item); p_data.Add(p_item.Data); } UpdateChildSelect(item, p_data); } }
/// <summary> /// 触发加载子节点事件 /// </summary> /// <param name="p_item"></param> /// <returns></returns> internal async Task OnLoadingChild(TvItem p_item) { if (LoadingChild == null) { return; } var args = new LoadingChildArgs(p_item); LoadingChild(this, args); await args.EnsureAllCompleted(); if (args.Children != null) { foreach (var item in args.Children) { TvItem ti = new TvItem(this, item, p_item); ti.ExpandedState = TvItemExpandedState.NotExpanded; p_item.Children.Add(ti); } RootItems.Invalidate(); } p_item.HasLoadedChildren = true; }
public LoadingChildArgs(TvItem p_item) { CurrentItem = p_item; }
void OnKeyDown(object sender, KeyRoutedEventArgs e) { if (SelectionMode == SelectionMode.Multiple || RootItems.Count == 0) { return; } TvItem row; switch (e.Key) { case VirtualKey.Down: row = SelectedRows.LastOrDefault(); if (row == null) { OnToggleSelected(RootItems[0]); } else { using (var ls = RootItems.GetExpandedItems().GetEnumerator()) { while (ls.MoveNext()) { if (ls.Current == row) { if (ls.MoveNext()) { OnToggleSelected(ls.Current); } else { OnToggleSelected(RootItems[0]); } break; } } } } e.Handled = true; return; case VirtualKey.Up: row = SelectedRows.LastOrDefault(); if (row == null) { OnToggleSelected(RootItems[0]); } else { using (var ls = RootItems.GetExpandedItems().GetEnumerator()) { if (ls.MoveNext()) { if (ls.Current == row) { SelectedItem = null; } else { while (true) { TvItem lastRow = ls.Current; if (ls.MoveNext()) { if (ls.Current == row) { OnToggleSelected(lastRow); break; } } } } } } } e.Handled = true; return; case VirtualKey.Enter: row = SelectedRows.LastOrDefault(); if (row != null) { row.OnClick(); } return; default: return; } }