public static bool WalkTreeViewItem(TreeView tree, TreeViewItem treeViewItem, object selectedValue) { if (treeViewItem.DataContext == selectedValue) { treeViewItem.SetValue(TreeViewItem.IsSelectedProperty, true); treeViewItem.Focus(); treeViewItem.BringIntoView(); return true; } var itemsHostProperty = treeViewItem.GetType().GetProperty("ItemsHost", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var itemsHost = itemsHostProperty?.GetValue(treeViewItem, null) as Panel; if (itemsHost == null) { return false; } foreach (var item in itemsHost.Children.OfType<TreeViewItem>()) { var oldExpanded = item.IsExpanded; item.IsExpanded = true; item.UpdateLayout(); if (WalkTreeViewItem(tree, item, selectedValue)) { return true; } item.IsExpanded = oldExpanded; } return false; }
/// <summary> /// Focus the first item in the TreeView. /// </summary> /// <returns>A value indicating whether the item was focused.</returns> private bool FocusFirstItem() { // Get the first item in the TreeView. TreeViewItem item = ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem; return((item != null) ? (item.IsEnabled && item.Focus()) || item.FocusDown() : false); }
public void TreeViewDefaultFocus(ItemsControl control) { foreach (Object oit in control.Items) { System.Windows.Controls.TreeViewItem tvi = control.ItemContainerGenerator.ContainerFromItem(oit) as TreeViewItem; tvi.IsExpanded = true; tvi.Focus(); break; } }
public void SelectItem(TreeViewItem item) { item.Focus(); item.IsSelected = true; item.BringIntoView(); }
private bool SelectTreeViewItem(ref TreeViewItem rootNode, string elementName) { bool isSelected = false; if (rootNode == null) return isSelected; if (!rootNode.IsExpanded) { rootNode.Focus(); rootNode.IsExpanded = true; } XmlNode tempNode = rootNode.Header as XmlNode; if (tempNode == null) { return isSelected; } if (string.Compare(tempNode.Name, elementName, true) == 0) { rootNode.IsSelected = true; rootNode.IsExpanded = true; isSelected = true; return isSelected; } else { for (int i = 0; i < rootNode.Items.Count; i++) { TreeViewItem childItem = rootNode.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem; isSelected = SelectTreeViewItem(ref childItem, elementName); if (isSelected) { break; } } return isSelected; } }
private bool HandleScrollByPage(bool up) { // NOTE: This implementation assumes that items are laid out // vertically and the Headers of the TreeViewItems appear above // their ItemsPresenter. The same assumptions are made in WPF. ScrollViewer scrollHost = ItemsControlHelper.ScrollHost; if (scrollHost != null) { double viewportHeight = scrollHost.ViewportHeight; double top; double bottom; (SelectedContainer.HeaderElement ?? SelectedContainer).GetTopAndBottom(scrollHost, out top, out bottom); TreeViewItem selected = null; TreeViewItem next = SelectedContainer; ItemsControl parent = SelectedContainer.ParentItemsControl; if (parent != null) { // We need to start at the root TreeViewItem if we're // scrolling up, but can start at the SelectedItem if // scrolling down. if (up) { while (parent != this) { TreeViewItem parentItem = parent as TreeViewItem; if (parentItem == null) { break; } ItemsControl grandparent = parentItem.ParentItemsControl; if (grandparent == null) { break; } next = parentItem; parent = grandparent; } } int index = parent.ItemContainerGenerator.IndexFromContainer(next); int count = parent.Items.Count; while (parent != null && next != null) { if (next.IsEnabled) { double delta; if (next.HandleScrollByPage(up, scrollHost, viewportHeight, top, bottom, out delta)) { // This item or one of its children was focused return(true); } else if (NumericExtensions.IsGreaterThan(delta, viewportHeight)) { // If the item doesn't fit on the page but it's // already selected, we'll select the next item // even though it doesn't completely fit into // the current view if (selected == SelectedContainer || selected == null) { return(up ? SelectedContainer.HandleUpKey() : SelectedContainer.HandleDownKey()); } break; } else { selected = next; } } index += up ? -1 : 1; if (0 <= index && index < count) { next = parent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem; } else if (parent == this) { // We just finished with the last item in the // TreeView next = null; } else { // Move up the parent chain to the next item while (parent != null) { TreeViewItem oldParent = parent as TreeViewItem; parent = oldParent.ParentItemsControl; if (parent != null) { count = parent.Items.Count; index = parent.ItemContainerGenerator.IndexFromContainer(oldParent) + (up ? -1 : 1); if (0 <= index && index < count) { next = parent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem; break; } else if (parent == this) { next = null; parent = null; } } } } } } if (selected != null) { if (up) { if (selected != SelectedContainer) { return(selected.Focus()); } } else { selected.FocusInto(); } } } return(false); }
/// <summary> /// Creates a playlist track list and navigation items /// </summary> /// <param name="playlist">The data for the playlist to create</param> /// <param name="select">Whether to select the playlist after it has been created</param> private void CreatePlaylist(PlaylistData playlist, bool select = true) { Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate() { if (playlist.ListConfig == null) { ViewDetailsConfig vdc = SettingsManager.CreateListConfig(); SettingsManager.InitViewDetailsConfig(vdc); vdc.HasNumber = true; vdc.Sorts.Add("asc:Title"); vdc.Sorts.Add("asc:Track"); vdc.Sorts.Add("asc:Album"); vdc.Sorts.Add("asc:Artist"); if (SettingsManager.SearchPolicy == SearchPolicy.Global) vdc.Filter = SettingsManager.FileListConfig.Filter; else if (SettingsManager.SearchPolicy == SearchPolicy.Partial && SettingsManager.Playlists.Count > 1) vdc.Filter = SettingsManager.Playlists[0].ListConfig.Filter; playlist.ListConfig = vdc; } PlaylistTrackLists.Add(playlist.Name, null); playlist.Tracks.CollectionChanged += new NotifyCollectionChangedEventHandler(PlaylistTracks_CollectionChanged); // create the item in the navigation tree TreeViewItem item = new TreeViewItem(); item.Selected += NavigationPane.Playlist_Selected; item.Drop += NavigationPane.Playlist_Drop; item.KeyDown += NavigationPlaylist_KeyDown; item.Tag = playlist.Name; item.Padding = new Thickness(8, 0, 0, 0); item.HorizontalAlignment = HorizontalAlignment.Stretch; item.HorizontalContentAlignment = HorizontalAlignment.Stretch; DockPanel dp = new DockPanel(); dp.LastChildFill = false; dp.HorizontalAlignment = HorizontalAlignment.Stretch; Image img = new Image(); img.Source = Utilities.GetIcoImage("pack://application:,,,/Platform/Windows 7/GUI/Images/Icons/DiscAudio.ico", 16, 16); img.Width = 16; img.Height = 16; img.Style = (Style)TryFindResource("HandHover"); dp.Children.Add(img); EditableTextBlock etb = new EditableTextBlock(); etb.EnteredEditMode += new EventHandler(EditableTextBlock_EnteredEditMode); etb.Text = playlist.Name; etb.Margin = new Thickness(5, 0, 5, 0); etb.Edited += NavigationPane.Playlist_Edited; etb.Canceled += NavigationPane.Playlist_Canceled; etb.HandHover = true; dp.Children.Add(etb); Image simg = new Image(); simg.Source = Utilities.GetIcoImage("pack://application:,,,/Platform/Windows 7/GUI/Images/Icons/Search.ico", 16, 16); simg.Width = 16; simg.Height = 16; simg.Margin = new Thickness(5, 0, 5, 0); simg.Visibility = Visibility.Collapsed; DockPanel.SetDock(simg, Dock.Right); dp.Children.Add(simg); item.Header = dp; item.ContextMenuOpening += new ContextMenuEventHandler(NavigationPane.Playlist_ContextMenuOpening); item.ContextMenuClosing += new ContextMenuEventHandler(NavigationPane.Playlist_ContextMenuClosing); item.ContextMenu = NavigationPane.playlistMenu; NavigationPane.Playlists.Items.Insert(NavigationPane.Playlists.Items.Count - 1, item); if (select) item.Focus(); // create list context menu items MenuItem miAdd = new MenuItem(); miAdd.Header = playlist.Name; miAdd.Click += new RoutedEventHandler(listMenuAddToPlaylist_Click); listMenuAddToPlaylist.Visibility = System.Windows.Visibility.Visible; listMenuAddToPlaylist.Items.Insert(listMenuAddToPlaylist.Items.Count - 1, miAdd); MenuItem miDel = new MenuItem(); miDel.Header = playlist.Name; miDel.Click += new RoutedEventHandler(listMenuRemoveFromPlaylist_Click); listMenuRemoveFromPlaylist.Visibility = System.Windows.Visibility.Visible; listMenuRemoveFromPlaylist.Items.Add(miDel); PlaybackControls.Search.AddPlaylist(playlist); NavigationPane.SetSearchIndicator("Playlist:" + playlist.Name, playlist.ListConfig); })); }
public virtual void FocusSelects() { TreeView view = new TreeView(); TreeViewItem first = new TreeViewItem { Header = "First" }; TreeViewItem second = new TreeViewItem { Header = "Second" }; view.Items.Add(first); view.Items.Add(second); TestAsync( view, () => first.Focus(), () => Assert.IsTrue(first.IsSelected, "First should be selected!"), () => second.Focus(), () => Assert.IsTrue(second.IsSelected, "Second should be selected!"), () => Assert.IsFalse(first.IsSelected, "First should not be selected now!")); }
public virtual void ExpanderButtonFocusActivatesSelection() { StackPanel root = new StackPanel(); TreeView view = new TreeView(); TreeViewItem item = new TreeViewItem { Header = "Item" }; Button button = new Button { Content = "Button" }; view.Items.Add(item); root.Children.Add(view); root.Children.Add(button); ToggleButton ExpanderButton = null; TestAsync( 5, root, () => item.Focus(), () => Assert.IsTrue(item.IsSelected, "Item should be selected!"), () => Assert.IsTrue(item.IsSelectionActive, "Selection should be active after focusing!"), () => button.Focus(), () => Assert.IsFalse(item.IsSelectionActive, "Selection should be inactive after focusing on something else!"), () => ExpanderButton = item.GetVisualChild("ExpanderButton") as ToggleButton, () => Assert.IsNotNull(ExpanderButton, "Failed to find template part ExpanderButton!"), () => ExpanderButton.Focus(), () => Assert.IsTrue(item.IsSelectionActive, "Selection should be active after focusing ExpanderButton!")); }
public virtual void SelectionInactiveOnLostFocus() { StackPanel root = new StackPanel(); TreeView view = new TreeView(); TreeViewItem item = new TreeViewItem { Header = "Item" }; view.Items.Add(item); Button other = new Button { Content = "Button" }; root.Children.Add(view); root.Children.Add(other); TestAsync( root, () => Assert.IsFalse(item.IsSelected, "Item should not be selected initially!"), () => Assert.IsFalse(item.IsSelectionActive, "Item should not have inactive selection initially!"), () => item.Focus(), () => Assert.IsTrue(item.IsSelected, "Item should be selected after focus!"), () => Assert.IsTrue(item.IsSelectionActive, "Item should not have inactive selection after focus!"), () => other.Focus(), () => Assert.IsTrue(item.IsSelected, "Item should be selected after focusing another element!"), () => Assert.IsFalse(item.IsSelectionActive, "Item should have an inactive selection after focusing another element!")); }
/// <summary> /// Fetches the structure of the layers and layer group names (in an XML document format) /// that are currently in the layer manager in WWT. /// </summary> /// <param name="displayResponse">show the response in response output</param> /// <param name="isLayerCreated">Checks if the call has come from Create layer</param> private void RefreshLayers(bool displayResponse, bool isLayerCreated) { try { string url = string.Format("http://{0}:5050/layerApi.aspx?cmd=layerlist", this.machineIp); string payload = string.Empty; string response = WWTRequest.Send(url, payload); if (displayResponse) { this.PrintSendAndReceive(url, response); this.PrintSendAndReceiveHeader("Refresh Layer List"); } this.layersTree.Items.Clear(); XmlDocument doc = new XmlDocument(); doc.LoadXml(response); XmlNode root = doc["LayerApi"]; XmlNode layersNode = root["LayerList"]; foreach (XmlElement element in layersNode.ChildNodes) { TreeViewItem node = new TreeViewItem(); node.Header = element.Attributes["Name"].Value; node.Name = element.Name; if (element.Attributes["ID"] != null) { node.Tag = element.Attributes["ID"].Value; } this.layersTree.Items.Add(node); node.IsExpanded = true; foreach (XmlElement child in element.ChildNodes) { // If the layer is not of spread sheet layer type, then ignore them. if (child.Attributes["Type"] != null && !child.Attributes["Type"].Value.Equals("SpreadSheetLayer", StringComparison.OrdinalIgnoreCase)) { continue; } TreeViewItem childNode = new TreeViewItem(); childNode.Header = child.Attributes["Name"].Value; childNode.Name = child.Name; if (child.Attributes["ID"] != null) { childNode.Tag = child.Attributes["ID"].Value; } node.Items.Add(childNode); // Select Earth. if (childNode.Header.ToString().Equals("Earth", StringComparison.OrdinalIgnoreCase) && !isLayerCreated) { childNode.IsSelected = true; } foreach (XmlElement grandChild in child.ChildNodes) { // If the layer is not of spread sheet layer type, then ignore them. if (grandChild.Attributes["Type"] != null && !grandChild.Attributes["Type"].Value.Equals("SpreadSheetLayer", StringComparison.OrdinalIgnoreCase)) { continue; } TreeViewItem grandChildNode = new TreeViewItem(); grandChildNode.Header = grandChild.Attributes["Name"].Value; grandChildNode.Name = grandChild.Name; if (grandChild.Attributes["ID"] != null) { grandChildNode.Tag = grandChild.Attributes["ID"].Value; if (isLayerCreated) { if (grandChild.Attributes["ID"].Value == layerId) { grandChildNode.IsSelected = true; layersTree.Focus(); grandChildNode.Focus(); childNode.IsExpanded = true; } } } childNode.Items.Add(grandChildNode); foreach (XmlElement greatGrandChild in grandChild.ChildNodes) { // If the layer is not of spread sheet layer type, then ignore them. if (greatGrandChild.Attributes["Type"] != null && !greatGrandChild.Attributes["Type"].Value.Equals("SpreadSheetLayer", StringComparison.OrdinalIgnoreCase)) { continue; } TreeViewItem greatGrandChildNode = new TreeViewItem(); greatGrandChildNode.Header = greatGrandChild.Attributes["Name"].Value; greatGrandChildNode.Name = greatGrandChild.Name; if (greatGrandChild.Attributes["ID"] != null) { greatGrandChildNode.Tag = greatGrandChild.Attributes["ID"].Value; if (isLayerCreated) { if (greatGrandChild.Attributes["ID"].Value == layerId) { greatGrandChildNode.IsSelected = true; layersTree.Focus(); greatGrandChildNode.Focus(); childNode.IsExpanded = true; grandChildNode.IsExpanded = true; } } } grandChildNode.Items.Add(greatGrandChildNode); } } } } if (this.cmbModes.SelectedIndex <= 0) { this.cmbModes.SelectedIndex = 0; } } catch (CustomException ex) { // Refresh has failed this.SetLayerButtons(false); this.SetCommonOperationsButtons(false); // WorldWide Telescope might not be open. throw ex; } }
private void HandleItemChanged (TreeViewItem tvi, WatchViewModel node) { if (tvi == null || node == null) return; // checks to see if the node to be selected is the same as the currently selected node // if so, then de-select the currently selected node. if (node == prevWatchViewModel) { this.prevWatchViewModel = null; if (tvi.IsSelected) { tvi.IsSelected = false; tvi.Focus(); } } else { this.prevWatchViewModel = node; } if (_vm.FindNodeForPathCommand.CanExecute(node.Path)) { _vm.FindNodeForPathCommand.Execute(node.Path); } }