【问题标题】:How to enable a button when a user types into a textbox如何在用户输入文本框时启用按钮
【发布时间】:2010-10-23 16:42:05
【问题描述】:

当用户在TextBox 中键入内容时,在 WPF 中启用Button 的最简单方法是什么?

【问题讨论】:

    标签: wpf button


    【解决方案1】:

    为每个笔划触发的 TextBox 添加一个回调。在此类回调中测试是否为空并启用/禁用按钮。

    【讨论】:

      【解决方案2】:

      使用简单命令

      <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));
          }     
      }
      

      欲了解更多信息,请参阅 Marlon Grechs SimpleCommand

      还可以查看来自http://blogs.msdn.com/llobo/archive/2009/05/01/download-m-v-vm-project-template-toolkit.aspx 的 MVVM 项目模板/工具包。它使用 DelegateCommand 进行命令,它应该是任何项目的一个很好的起始模板。

      【讨论】:

        【解决方案3】:

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

        例如,使用通用 Int 到 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}}/>
        

        HTH,

        丹尼斯

        【讨论】:

          【解决方案4】:

          使用触发器!

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

          【讨论】:

            【解决方案5】:

            为什么大家都把事情搞得这么复杂!

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

            没有其他需要......

            【讨论】:

            • 你,爵士,真是个天才!
            • 这太棒了!如果你有更多的文本框怎么办?
            • 我几乎做到了,我得到的最接近的是 {Binding ElementName=yarp, Path=Text} 因为我确定它可以将 Text 属性转换为 bool,所以为什么还要输入“.Length”:D
            • 简单优雅的解决方案。这应该是公认的答案。
            • 哇,这是在 xaml 中做到这一点的最好和优雅的方式。
            【解决方案6】:

            关键在于绑定本身..

            添加 UpdateSourceTrigger = PropertyChanged

            这是最简单的解决方案

            【讨论】:

              猜你喜欢
              • 2015-05-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-03-05
              • 1970-01-01
              相关资源
              最近更新 更多