0
<UserControl  UIHelper:FocusExtension.IsFocused="{Binding FocusUserControl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Focusable="True" IsTabStop="True">

here I added an IsFocused prop to maintain the focus of usercontrol

 public static class FocusExtension
    {
        public static bool GetIsFocused(DependencyObject obj)
        {

            return (bool)obj.GetValue(IsFocusedProperty);

        }

        public static void SetIsFocused(DependencyObject obj, bool value)
        {
            obj.SetValue(IsFocusedProperty, value);
        }

        public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached(
                "IsFocused", typeof(bool), typeof(FocusExtension),
                new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

        private static void OnIsFocusedPropertyChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            var uie = (UIElement)d;
            if ((bool)e.NewValue)
            {
                 uie.Focus();
            }
        }
    }

this is my FocusExtension class

public bool FocusUserControl
        {
            get { return focusUserControl; }
            set
            {
                focusUserControl = value;
                OnPropertyChanged(new PropertyChangedEventArgs("FocusUserControl"));
            }
        }

this is my VM prop. which is binded, now in user control, I have a button. when I click on it a new user control will. open at that time I need to update the FocusUserControl to false. as the focus is not on that usercontrol anymore. I am stuck here please suggest something thanks for ur help.

**

Update i added something like this

**

 private static void OnIsFocusedPropertyChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            uie = (UIElement)d;
            if ((bool)e.NewValue)
            {
                uie.LostKeyboardFocus += LostFocus;
                uie.Focus();
            }

        }
        static void LostFocus(object sender, RoutedEventArgs e)
        {
            uie.LostKeyboardFocus -= LostFocus;
            // Here i need to update `FocusUserControl` to false 
        }

in LostFocus i just need to update FocusUserControl to false. how can i do it? really use some help.

Avinash Reddy
  • 937
  • 8
  • 22
  • please add some punctuation to your question it is hard to read specifically dots colons dashes and question marks are super useful thanks also your dp is not subscribing to any control event looks like it is working only one way – vasily.sib Apr 04 '19 at 04:12
  • I think I want to subscribe to `LostFocus` not sure. is that right? and yes it is working in one way from VM to ui @vasily.sib – Avinash Reddy Apr 04 '19 at 04:18
  • Attached property is not the best solution for this. What you actualy want is to trigger some action on view's event. Take a look at [this](https://stackoverflow.com/questions/14122823/how-to-trigger-viewmodel-command-for-a-specific-button-events) question and specifically at acepted answer - it is not about `LostFocus` event but can be modified easily. – vasily.sib Apr 04 '19 at 05:16
  • @vasily.sib As my application is based on warehouse I think I can't use it he is my exact problem, in user control, I have a button which will open another user control and they will perform some operations. once they close it they will use the scanner to give input as my user control is not focused on current user control its not takeing input – Avinash Reddy Apr 04 '19 at 05:27
  • @vasily.sib if user clicks on current usercontrol and start scanning it will work fine as the focus changed so there wont be mouse clicks i need to focus it through code – Avinash Reddy Apr 04 '19 at 05:28
  • @vasily.sib please check the update once can update the prop – Avinash Reddy Apr 04 '19 at 06:34
  • if anyone looking for ans [this is the answer](https://stackoverflow.com/a/16966029/7765929) – Avinash Reddy Apr 04 '19 at 07:56
  • If there are multiple controls in the usercontrol use IsKeyboardFocusWithinChanged. – Andy Apr 04 '19 at 11:12

0 Answers0