2009-05-04 9 views

risposta

0

utilizzare il comando semplice

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

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

Ecco il codice di esempio nella vista del modello

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

Per ulteriori informazioni vedere Marlon Grechs SimpleCommand

Verificate anche il modello MVVM progetto/Toolkit da http://blogs.msdn.com/llobo/archive/2009/05/01/download-m-v-vm-project-template-toolkit.aspx. Usa il comando DelegateCommand e dovrebbe essere un ottimo modello di partenza per qualsiasi progetto.

-1

Aggiungi un callback al TextBox che si attiva in ogni tratto. Verificare la presenza di vuoto in tale callback e abilitare/disabilitare il pulsante.

2

SE non si stavano utilizzando i comandi, un'altra alternativa sta utilizzando un convertitore.

Ad esempio, utilizzando un Int generico per Bool convertitore:

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

Poi sui pulsanti proprietà IsEnabled:

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

HTH,

Dennis

2

utilizzare un trigger !

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

Perché tutti fanno le cose così complicate!

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

Nient'altro necessario ......

+4

+1 semplice e funzionante - grazie! –

+0

U, ser, sei un genio! –

+0

Vorrei poterlo trasmettere ancora più – cppguy

0

La chiave di questo è in sé vincolante ..

Aggiungi UpdateSourceTrigger = PropertyChanged

questo è la soluzione più semplice

Problemi correlati