代码之家  ›  专栏  ›  技术社区  ›  Steve

如何在用户键入文本框时启用按钮

  •  17
  • Steve  · 技术社区  · 17 年前

    Button 当用户将某个内容输入到 TextBox

    6 回复  |  直到 14 年前
        1
  •  127
  •   user253175    16 年前

    为什么每个人都把事情弄得这么复杂!

        <TextBox x:Name="TB"/>
        <Button IsEnabled="{Binding ElementName=TB,Path=Text.Length}">Test</Button>
    

        2
  •  3
  •   Bert    16 年前

    <TextBox x:Name="txt_Titel />
    <Button Content="Transfer" d:IsLocked="True">
      <Button.Style>
        <Style>
          <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=txt_Titel, Path=Text}" Value="">
             <Setter Property="Button.IsEnabled" Value="false"/>
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </Button.Style>
    </Button>
    
        3
  •  2
  •   Dennis    17 年前

    如果您没有使用命令,另一种选择是使用转换器。

    例如,使用通用Int-to-Bool转换器:

      [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
      }
    

    然后在按钮IsEnabled属性上:

    <Button IsEnabled={Binding ElementName=TextBoxName, Path=Text.Length, Converter={StaticResource IntToBoolConverter}}/>
    

    嗯,,

        4
  •  0
  •   Ohad Horesh    11 年前

    <TextBox Text={Binding Path=TitleText}/>
    
    <Button Command="{Binding Path=ClearTextCommand}" Content="Clear Text"/>
    

    下面是视图模型中的示例代码

    public class MyViewModel : INotifyPropertyChanged
    {
        public ICommand ClearTextCommand { get; private set; }
    
        private string _titleText; 
        public string TitleText
        {
            get { return _titleText; }
            set
            {
                if (value == _titleText)
                    return;
    
                _titleText = value;
                this.OnPropertyChanged("TitleText");
            }
        }   
    
        public MyViewModel()
        {
            ClearTextCommand = new SimpleCommand
                {
                    ExecuteDelegate = x => TitleText="",
                    CanExecuteDelegate = x => TitleText.Length > 0
                };  
        }            
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }     
    }
    

    SimpleCommand

    另外,还可以从中查看MVVM项目模板/工具包 http://blogs.msdn.com/llobo/archive/2009/05/01/download-m-v-vm-project-template-toolkit.aspx . 它使用DelegateCommand进行指挥,对于任何项目来说,它都应该是一个很好的开始模板。

        5
  •  0
  •   George30    9 年前

    这个问题的关键在于绑定本身。。

    添加UpdateSourceTrigger=PropertyChanged

        6
  •  -2
  •   Anzurio    17 年前

    向文本框添加一个回调,该文本框在每次笔划时触发。测试该回调中是否为空,并启用/禁用按钮。