private void ChangeSelection(TreeViewItemViewModel selectedNode) { Task.Factory.StartNew(() => { var target = _nodes .Where(x => x.Value.Item.FullName.StartsWith(selectedNode.Item.FullName)) .Where(x => x.Value.Level != selectedNode.Level); foreach (var child in target) { child.Value.IsSelected = selectedNode.IsSelected; } Action <TreeViewItemViewModel> changeParentsCheckState = null; changeParentsCheckState = (TreeViewItemViewModel n) => { if (n.Parent == null) { return; } TreeViewItemViewModel parent; if (!n.Parent.TryGetTarget(out parent)) { return; } if (parent.Children == null) { return; } int count = parent.Children.Count; int falseCount = parent.Children.Count(x => x.IsSelected.HasValue && !x.IsSelected.Value); int trueCount = parent.Children.Count(x => x.IsSelected.HasValue && x.IsSelected.Value); if (count == trueCount) { parent.IsSelected = true; } else if (count == falseCount) { parent.IsSelected = false; } else { parent.IsSelected = null; } changeParentsCheckState(parent); }; changeParentsCheckState(selectedNode); }); }
private void OpenDirectory() { foreach (var node in _nodes) { node.Value.Cleanup(); } _nodes.Clear(); var dialog = new BrowseForFolder(); dialog.Options = 1; //dialog.RootFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); dialog.Title = "フォルダを選択して下さい。"; dialog.ShowDialog(); if (string.IsNullOrEmpty(dialog.SelectedPath) || !Directory.Exists(dialog.SelectedPath)) { return; } InputDirectoryPath = dialog.SelectedPath; var root = new TreeViewItemViewModel(); var parent = new DirectoryInfo(InputDirectoryPath); root.Item = parent; _nodes[root.Item.FullName] = root; Tree.Root = CollectionViewSource.GetDefaultView(new [] { root }); ExportTree.Root = Tree.Root; Task.Factory.StartNew(() => { foreach (var child in parent.EnumerateFileSystemInfos()) { if ((child.Attributes & (FileAttributes.Hidden | FileAttributes.System)) > 0) { continue; } try { CreateTree(_nodes, child, parent); } catch (UnauthorizedAccessException) { _nodes.Remove(child.FullName); } } }); }
private void CreateTree(Dictionary <string, TreeViewItemViewModel> nodes, FileSystemInfo current, FileSystemInfo parent) { var node = new TreeViewItemViewModel(); nodes[current.FullName] = node; node.Item = current; if (parent != null) { var parentNode = nodes[parent.FullName]; node.Parent = new WeakReference <TreeViewItemViewModel>(parentNode); Action <TreeViewItemViewModel, TreeViewItemViewModel> add = (TreeViewItemViewModel p, TreeViewItemViewModel c) => p.Children.Add(c); UIDispatcher.Invoke(add, DispatcherPriority.Normal, parentNode, node); } if (!(current is DirectoryInfo)) { return; } foreach (var child in (current as DirectoryInfo).EnumerateFileSystemInfos()) { if ((child.Attributes & (FileAttributes.Hidden | FileAttributes.System)) > 0) { continue; } try { CreateTree(nodes, child, current); } catch (UnauthorizedAccessException) { nodes.Remove(child.FullName); } } }