ユーザーがテキストボックスに入力するときにボタンを有効にする方法

StackOverflow https://stackoverflow.com/questions/821121

  •  03-07-2019
  •  | 
  •  

質問

ユーザーが TextBox に何かを入力したときに Button を有効にするWPFの最も簡単な方法は何ですか?

役に立ちましたか?

解決

単純なコマンドを使用

<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-mv-vm-project-template-toolkit.aspx 。コマンドにはDelegateCommandを使用し、あらゆるプロジェクトの優れた開始テンプレートである必要があります。

他のヒント

なぜ誰もが物事をそんなに複雑にしているのですか!

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

他に必要なものはありません......

コマンドを使用していなかった場合は、別の方法としてコンバーターを使用します。

たとえば、汎用の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、

デニス

トリガーを使用してください!

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

このキーはバインディング自体にあります。

UpdateSourceTrigger = PropertyChangedを追加

これは最も簡単なソリューションです

すべてのストロークで発生するコールバックをTextBoxに追加します。そのようなコールバックで空かどうかをテストし、ボタンを有効/無効にします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top