Set focus on TextBox in WPF from view model

后端 未结 21 2215
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:21

I have a TextBox and a Button in my view.

Now I am checking a condition upon button click and if the condition turns out to be false, displ

21条回答
  •  囚心锁ツ
    2020-11-22 06:54

    For Silverlight:

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Interactivity;
    
    namespace MyProject.Behaviors
    {
        public class FocusBehavior : Behavior
        {
            protected override void OnAttached()
            {
                this.AssociatedObject.Loaded += AssociatedObject_Loaded;
                base.OnAttached();
            }
    
            private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
            {
                this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
                if (this.HasInitialFocus || this.IsFocused)
                {
                    this.GotFocus();
                }
            }
    
            private void GotFocus()
            {
                this.AssociatedObject.Focus();
                if (this.IsSelectAll)
                {
                    if (this.AssociatedObject is TextBox)
                    {
                        (this.AssociatedObject as TextBox).SelectAll();
                    }
                    else if (this.AssociatedObject is PasswordBox)
                    {
                        (this.AssociatedObject as PasswordBox).SelectAll();
                    }
                    else if (this.AssociatedObject is RichTextBox)
                    {
                        (this.AssociatedObject as RichTextBox).SelectAll();
                    }
                }
            }
    
            public static readonly DependencyProperty IsFocusedProperty =
                DependencyProperty.Register(
                    "IsFocused",
                    typeof(bool),
                    typeof(FocusBehavior),
                    new PropertyMetadata(false, 
                        (d, e) => 
                        {
                            if ((bool)e.NewValue)
                            {
                                ((FocusBehavior)d).GotFocus();
                            }
                        }));
    
            public bool IsFocused
            {
                get { return (bool)GetValue(IsFocusedProperty); }
                set { SetValue(IsFocusedProperty, value); }
            }
    
            public static readonly DependencyProperty HasInitialFocusProperty =
                DependencyProperty.Register(
                    "HasInitialFocus",
                    typeof(bool),
                    typeof(FocusBehavior),
                    new PropertyMetadata(false, null));
    
            public bool HasInitialFocus
            {
                get { return (bool)GetValue(HasInitialFocusProperty); }
                set { SetValue(HasInitialFocusProperty, value); }
            }
    
            public static readonly DependencyProperty IsSelectAllProperty =
                DependencyProperty.Register(
                    "IsSelectAll",
                    typeof(bool),
                    typeof(FocusBehavior),
                    new PropertyMetadata(false, null));
    
            public bool IsSelectAll
            {
                get { return (bool)GetValue(IsSelectAllProperty); }
                set { SetValue(IsSelectAllProperty, value); }
            }
    
        }
    }
    

    LoginViewModel.cs:

        public class LoginModel : ViewModelBase
        {
            ....
    
            private bool _EmailFocus = false;
            public bool EmailFocus
            {
                get
                {
                    return _EmailFocus;
                }
                set
                {
                    if (value)
                    {
                        _EmailFocus = false;
                        RaisePropertyChanged("EmailFocus");
                    }
                    _EmailFocus = value;
                    RaisePropertyChanged("EmailFocus");
                }
            }
           ...
       }
    

    Login.xaml:

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:beh="clr-namespace:MyProject.Behaviors"
    
    
        
            
        
    
    

    OR

    
        
            
        
    
    

    To set the focus should just do it in code:

    EmailFocus = true;
    

    Remember that this plugin is part of an html page, so other controls in the page might have the focus

    if (!Application.Current.IsRunningOutOfBrowser)
    {
        System.Windows.Browser.HtmlPage.Plugin.Focus();
    }
    

提交回复
热议问题