Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:42:09+00:00 2026-06-11T00:42:09+00:00

I move focus to the Popup on its opening: wcl:FocusHelper.IsFocused={Binding RelativeSource={RelativeSource Self}, Path=IsOpen} FocusHelper

  • 0

I move focus to the Popup on its opening:

wcl:FocusHelper.IsFocused="{Binding RelativeSource={RelativeSource Self}, Path=IsOpen}"

FocusHelper class code:

public static class FocusHelper
{
     public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached("IsFocused", typeof(bool?), typeof(FocusHelper), new FrameworkPropertyMetadata(IsFocusedChanged));

        public static bool? GetIsFocused(DependencyObject element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            return (bool?)element.GetValue(IsFocusedProperty);
        }

        public static void SetIsFocused(DependencyObject element, bool? value)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            element.SetValue(IsFocusedProperty, value);
        }

        private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var fe = (FrameworkElement)d;

            if (e.OldValue == null)
            {
                fe.GotFocus += ElementGotFocus;
                fe.LostFocus += ElementLostFocus;
                fe.IsVisibleChanged += ElementIsVisibleChanged;
            }

            if (e.NewValue == null)
            {
                fe.GotFocus -= ElementGotFocus;
                fe.LostFocus -= ElementLostFocus;
                fe.IsVisibleChanged -= ElementIsVisibleChanged;
                return;
            }

            if ((bool)e.NewValue)
            {
                fe.SetFocusWithin();
            }
        }

        private static void ElementIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var fe = (FrameworkElement)sender;

            if (fe.IsVisible 
                && (bool)(((FrameworkElement) sender).GetValue(IsFocusedProperty))) // Bring focus to just become visible element.
                fe.Focus();
        }

        private static void ElementGotFocus(object sender, RoutedEventArgs e)
        {
            ((FrameworkElement)sender).SetCurrentValue(IsFocusedProperty, true);
        }

        private static void ElementLostFocus(object sender, RoutedEventArgs e)
        {
            ((FrameworkElement)sender).SetCurrentValue(IsFocusedProperty, false);
        }

        /// <summary>
        /// Tries to set focus to the element or any other element inside this one.
        /// Tab index is respected
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static bool SetFocusWithin(this DependencyObject element)
        {
            if (element == null)
                throw new ArgumentNullException("element");

            var inputElement = element as IInputElement;
            if (inputElement == null || !inputElement.Focus())
            {
                var children = element.GetVisualChildrenSortedByTabIndex().Where(child => !(child is Control) || (bool)child.GetValue(Control.IsTabStopProperty) );
                return children.Any(SetFocusWithin);
            }

            return true;
        }
    }

ElementTreeHelper class part :

public static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject parent)
        {
            if (parent == null)
                throw new ArgumentNullException("parent");

            var count = VisualTreeHelper.GetChildrenCount(parent);
            for (var i = 0; i < count; i++)
                yield return VisualTreeHelper.GetChild(parent, i);
        }

        public static IEnumerable<DependencyObject> GetVisualChildrenSortedByTabIndex(this DependencyObject parent)
        {
            if (parent == null)
                throw new ArgumentNullException("parent");

            return parent.GetVisualChildren().OrderBy(KeyboardNavigation.GetTabIndex);
        }

The problem is that
var count = VisualTreeHelper.GetChildrenCount(parent) == 0 when parent is Popup.

UPDATE

The answer is here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:42:10+00:00Added an answer on June 11, 2026 at 12:42 am

    The Popup doesn’t host the Child within itself. Instead it creates a PopupRoot (internal class) that is the root visual of a new HWND created to host the popup’s content within a separate top level window (or child window in xbap). The Child of the Popup is hosted within an AdornerDecorator within that PopupRoot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After opening a new tab on my site, I want to move the focus
I've got a WPF view with two textboxes. I'd like to automatically move focus
I would like to move a circle along a circular path using HTML/CSS/JavaScript. Is
I have to move focus to parent frame using JAVASCRIPT:focus(). The thing is that
Can't move focus to the control because it is invisible, not enabled, or of
I am trying to move focus to a validation error message using Jquery. By
In my WPF application, I want to move focus to the controls using up,
I'm opening new window with next code: window.open(url,pageName1,menubar=1,resizable=1,scrollbars=1,status=yes,width=1050,height=820); so window is not modal In
Listbox in a DataTemplate <ListBox ItemsSource="{Binding Path=(Duties:IPicker.Choices)}" SelectedValue="{Binding Path=(Duties:IPicker.Selection), ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"> <ListBox.ItemsPanel> <ItemsPanelTemplate>
I want to move the focus from the textbox to an ajax combobox on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.