コード例 #1
0
        private void SetFocusOnFirstButton()
        {
            var firstButton = VisualTreeHelpers.FindVisualChild <Button>(AnswerButtons.ItemContainerGenerator.ContainerFromIndex(0));

            if (firstButton != null)
            {
                FocusManager.SetFocusedElement(AnswerButtons, firstButton);
                firstButton.Dispatcher.BeginInvoke((Action) delegate
                {
                    firstButton.Focus();
                    Keyboard.Focus(firstButton);
                }, DispatcherPriority.Input);
            }
        }
コード例 #2
0
        /// <summary>
        /// Recursively search for an item in this subtree.
        /// </summary>
        /// <param name="container">
        /// The parent ItemsControl. This can be a TreeView or a TreeViewItem.
        /// </param>
        /// <param name="item">
        /// The item to search for.
        /// </param>
        /// <returns>
        /// The TreeViewItem that contains the specified item.
        /// </returns>
        public static TreeViewItem?GetTreeViewItem(ItemsControl container, object item)
        {
            if (container == null)
            {
                return(null);
            }

            if (container.DataContext == item)
            {
                return(container as TreeViewItem);
            }

            // Expand the current container
            if (container is TreeViewItem tvi && !tvi.IsExpanded)
            {
                container.SetValue(TreeViewItem.IsExpandedProperty, true);
            }

            // Try to generate the ItemsPresenter and the ItemsPanel.
            // by calling ApplyTemplate.  Note that in the
            // virtualizing case even if the item is marked
            // expanded we still need to do this step in order to
            // regenerate the visuals because they may have been virtualized away.

            container.ApplyTemplate();
            ItemsPresenter?itemsPresenter = (ItemsPresenter)container.Template.FindName("ItemsHost", container);

            if (itemsPresenter != null)
            {
                itemsPresenter.ApplyTemplate();
            }
            else
            {
                // The Tree template has not named the ItemsPresenter,
                // so walk the descendents and find the child.
                itemsPresenter = VisualTreeHelpers.FindVisualChild <ItemsPresenter>(container);
                if (itemsPresenter == null)
                {
                    container.UpdateLayout();

                    itemsPresenter = VisualTreeHelpers.FindVisualChild <ItemsPresenter>(container);
                }
            }

            Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);

            for (int i = 0, count = container.Items.Count; i < count; i++)
            {
                TreeViewItem subContainer;
                if (itemsHostPanel is MyVirtualizingStackPanel virtualizingPanel)
                {
                    // Bring the item into view so
                    // that the container will be generated.
                    virtualizingPanel.BringIntoView(i);

                    subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);
                }
                else
                {
                    subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);

                    // Bring the item into view to maintain the
                    // same behavior as with a virtualizing panel.
                    subContainer.BringIntoView();
                }

                if (subContainer != null)
                {
                    // Search the next level for the object.
                    TreeViewItem?resultContainer = GetTreeViewItem(subContainer, item);
                    if (resultContainer != null)
                    {
                        return(resultContainer);
                    }

                    // The object is not under this TreeViewItem
                    // so collapse it.
                    subContainer.IsExpanded = false;
                }
            }

            return(null);
        }