How to enable a button when a user types into a textbox

后端 未结 6 570
礼貌的吻别
礼貌的吻别 2020-12-25 15:06

What is the simplest way in WPF to enable a Button when the user types something into a TextBox?

6条回答
  •  情深已故
    2020-12-25 15:41

    IF you were not using Commands, another alternative is using a Converter.

    For example, using a generic Int to Bool converter:

      [ValueConversion(typeof(int), typeof(bool))]
      public class IntToBoolConverter : IValueConverter
      {
        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          try
          {
            return (System.Convert.ToInt32(value) > 0);
          }
          catch (InvalidCastException)
          {
            return DependencyProperty.UnsetValue;
          }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          return System.Convert.ToBoolean(value) ? 1 : 0;
        }
    
        #endregion
      }
    

    Then on the buttons IsEnabled property:

    HTH,

    Dennis

提交回复
热议问题