IsFocused Property

6 posts, 0 answers
  1. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 14 Jul 2014 Link to this post

    Hi,

    using an attached behavior I tried to set the focus on a RadMaskedTextInput.
    It didn't work as expected and so I tried to set it manually.

    Doing this I found that IsFocused is not accessible as Dependency Property.
    The question now - how to handle focusing in MVVM with RadMaksedTextInput.

    My normal code looks something like this:
    <TextBox Text="{Binding FirstName}" myExt:FocusExtension.IsFocused="{Binding IsFirstNameFocused}"...

    Thanks
    Manfred
  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 14 Jul 2014 in reply to ManniAT Link to this post

    Sorry about my first post - I messed up something.

    The problem is - I have an attached property which works fine with Windows (WPF) and several other telerik WPF Controls.

    private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            var fe = (FrameworkElement)d;
     
            if(e.OldValue == null) {
                fe.GotFocus += FrameworkElement_GotFocus;
                fe.LostFocus += FrameworkElement_LostFocus;
            }
     
            if(!fe.IsVisible) {
                fe.IsVisibleChanged += new DependencyPropertyChangedEventHandler(fe_IsVisibleChanged);
            }
            if(!fe.IsEnabled) {
                fe.IsEnabledChanged += fe_IsEnabledChanged;
            }
            if((bool)e.NewValue) {
                fe.Focus();
            }
        }

    By messing arround with not getting a caret I learned that the caret is there when I place a breakpoint in the last statement in this code (fe.Focus()).
    So I first thought this is a "timing problem" - but later I found it is not.
    I start my app - no caret in the RadMasked...
    I Alt+Tab to a different application and back to mine - caret is there.

    So just "activating" my app places the caret - but my extension (a call to RadMaske..Instance.Focus() does not really work.

    Manfred
  3. UI for WPF is Visual Studio 2017 Ready
  4. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 17 Jul 2014 Link to this post

    Hi Manfred,

    Thank you for the provided code snippet.

    I prepared a small project that uses your code, but I was not able to reproduce the reported behavior. Can you please take a look at the attached project and let me know if I am missing something?

    As a side note, when you subscribe for events at runtime and it is possible to subscribe for them multiple times during the application's lifespan, it is recommended to unsubscribe for those events first. For example:
    if(!fe.IsEnabled) {
        fe.IsEnabledChanged -= fe_IsEnabledChanged;
        fe.IsEnabledChanged += fe_IsEnabledChanged;
    }

    Thank you for any help you can provide.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  5. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 18 Jul 2014 Link to this post

    Hi Martin,

    first of all about your hint... the handler gets removed in the handler itself so this is not the problem.
    The idea behind - if a control is not enabled and has this focused property it should get the focus as soon as it gets enabled.
    The same for visible.
    The reason for this - I can have two (or more) controls with is focused.
    Some of them are hidden (nothing happens) and the last visible / enabled one will get the focus.

    I checked your sample - and it works!!
    So this helped me a bit further - it seems as if the problem has something to do with visibility.
    As soon as I remove this "visibility" handler (just ignore the visible fact) everything works.

    The visible handler gets called - also the fe.Focus() inside - but....
    I commented it in code - conclusion - if I call fe.Focus() in the visible changed handler I get the "bad effect".

    public static class FocusExtension {
            /// <summary>
            /// The is focused property
            /// </summary>
            public static readonly DependencyProperty IsFocusedProperty =
                DependencyProperty.RegisterAttached("IsFocused", typeof(bool?), typeof(FocusExtension), new FrameworkPropertyMetadata(IsFocusedChanged));
     
            /// <summary>
            /// Gets the is focused.
            /// </summary>
            /// <param name="element">The element.</param>
            /// <returns></returns>
            /// <exception cref="System.ArgumentNullException">element</exception>
            public static bool? GetIsFocused(DependencyObject element) {
                if(element == null) {
                    throw new ArgumentNullException("element");
                }
     
                return (bool?)element.GetValue(IsFocusedProperty);
            }
     
            /// <summary>
            /// Sets the is focused.
            /// </summary>
            /// <param name="element">The element.</param>
            /// <param name="value">The value.</param>
            /// <exception cref="System.ArgumentNullException">element</exception>
            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 -= FrameworkElement_GotFocus;
                    //fe.LostFocus -= FrameworkElement_LostFocus;
     
                    fe.GotFocus += FrameworkElement_GotFocus;
                    fe.LostFocus += FrameworkElement_LostFocus;
                }
                if(!fe.IsVisible || !fe.IsEnabled) {
                    if(!fe.IsVisible) {
                        fe.IsVisibleChanged += fe_IsVisibleChanged;
                    }
                    if(!fe.IsEnabled) {
                        fe.IsEnabledChanged += fe_IsEnabledChanged;
                    }
                    //comment this out to force focus even if not visible / enabled
                    //return;
                }
                if((bool)e.NewValue) {
                    fe.Focus();
                }
            }
     
            private static void fe_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
                var fe = (FrameworkElement)sender;
                if(fe.IsVisible && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty)) {
                    fe.IsVisibleChanged -= fe_IsVisibleChanged;
                    //commenting this - makes it work
                    fe.Focus();
                }
            }
     
            private static void fe_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) {
                var fe = (FrameworkElement)sender;
                if(fe.IsEnabled && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty)) {
                    fe.IsEnabledChanged -= fe_IsEnabledChanged;
                    fe.Focus();
                }
            }
     
            private static void FrameworkElement_GotFocus(object sender, RoutedEventArgs e) {
                if(e.Source == e.OriginalSource) {
                    ((FrameworkElement)sender).SetValue(IsFocusedProperty, true);
                }
            }
     
            private static void FrameworkElement_LostFocus(object sender, RoutedEventArgs e) {
                if(e.Source == e.OriginalSource) {
                    ((FrameworkElement)sender).SetValue(IsFocusedProperty, false);
                }
            }
        }
    }

    By the way - it works with other controls...
    Event with telerik controls - just the masked....

    Manfred
  6. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 22 Jul 2014 Link to this post

    Hi Manfred,

    Thank you for the explanation and the code snippet.

    Indeed when the Visibility of the masked input is initially set to Collapsed and you change it to Visible and then focus the control, the carret is missing although the control is focused. I will forward the issue for investigation to our developers and as soon as we have more information on that matter we will contact you again.

    Thank you in advance for your understanding.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  7. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 24 Jul 2014 Link to this post

    Hi Manfred,

    We investigated this issue on our side. Basically, the Style / Template of the RadMaskedInputBase includes two textboxes , button, and several more UI Elements. When you make this control Visible and then immediately Focus it, there is no enough time between these two operations. In the moment of focusing, the underlying TextBox is not loaded completely. What you can do is to dispatch the focusing like so:
    private void Button_Click(object sender, RoutedEventArgs e)
          {
              this.maskedInput.Visibility = Visibility.Visible;
              Dispatcher.BeginInvoke(new Action(() => { this.maskedInput.Focus(); }), DispatcherPriority.Loaded);

    Let us know if this helps you procedd further.

    Regards,
    Petar Mladenov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WPF is Visual Studio 2017 Ready