Set focus on TextBox in WPF from view model

后端 未结 21 2511
爱一瞬间的悲伤
爱一瞬间的悲伤 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:48

    For those trying to use Anvaka's solution above, I was having issues with the binding only working the first time, as lostfocus would not update the property to false. You can manually set the property to false and then true every time, but a better solution could be to do something like this in your property:

    bool _isFocused = false;
        public bool IsFocused 
        {
            get { return _isFocused ; }
            set
            {
                _isFocused = false;
                _isFocused = value;
                base.OnPropertyChanged("IsFocused ");
            }
        }
    

    This way you only ever need to set it to true, and it will get focus.

提交回复
热议问题