Set focus on TextBox in WPF from view model

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

    Nobody seems to have included the final step to make it easy to update attributes via binded variables. Here's what I came up with. Let me know if there is a better way of doing this.

    XAML

        
    
        

    ViewModel

        public class LoginModel : ViewModelBase
        {
    
        public string txtLabel_IsFocused { get; set; }                 
        public string butEdit_IsEnabled { get; set; }                
    
    
        public void SetProperty(string PropertyName, string value)
        {
            System.Reflection.PropertyInfo propertyInfo = this.GetType().GetProperty(PropertyName);
            propertyInfo.SetValue(this, Convert.ChangeType(value, propertyInfo.PropertyType), null);
            OnPropertyChanged(PropertyName);
        }                
    
    
        private void Example_function(){
    
            SetProperty("butEdit_IsEnabled", "False");
            SetProperty("txtLabel_IsFocused", "True");        
        }
    
        }
    

提交回复
热议问题