Comment activer un bouton lorsqu'un utilisateur tape dans une zone de texte

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

  •  03-07-2019
  •  | 
  •  

Question

Quel est le moyen le plus simple dans WPF d’activer un bouton lorsque l’utilisateur tape quelque chose dans un TextBox ?

Était-ce utile?

La solution

Utiliser la commande simple

<TextBox Text={Binding Path=TitleText}/>

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

Voici l'exemple de code dans le modèle de vue

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

Pour plus d'informations, voir Marlon Grechs SimpleCommand

Consultez également le modèle de projet / la boîte à outils MVVM sur http://blogs.msdn.com/llobo/archive/2009/05/01/download-mv-vm-project-template-toolkit.aspx . Il utilise DelegateCommand pour les commandes et devrait constituer un excellent modèle de départ pour tout projet.

Autres conseils

Pourquoi tout le monde complique-t-il les choses!

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

Rien d’autre n’est nécessaire ......

SI vous n'utilisiez pas de commandes, une autre alternative consiste à utiliser un convertisseur.

Par exemple, en utilisant un convertisseur générique Int vers 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
  }

Puis sur les boutons, propriété IsEnabled:

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

HTH,

Dennis

Utilisez un déclencheur!

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

La clé est la reliure elle-même.

Ajouter UpdateSourceTrigger = PropertyChanged

c'est la solution la plus simple

Ajoutez un rappel à la zone de texte qui se déclenche à chaque coup. Testez le vide dans un tel rappel et activez / désactivez le bouton.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top