2010-12-11 6 views
0

Ich habe ein Problem mit der Fehlerbehandlung in Sicht. Ich benutze caliburn.micro und MEF.Fehlerbehandlung in WPF-MVVM/deaktiviert Schaltfläche

Meine VM sieht wie folgt aus:

[Export(typeof(IShellViewModel))] 
public class ShellViewModel : PropertyChangedBase, IShellViewModel,IDataErrorInfo 
{ 

    #region Private members 

    private User _user; 
    private Dictionary<string, bool> _validProperties; 
    private bool _allPropertiesValid; 

    #endregion 

    #region Private methods 

    private void ValidateProperties() 
    { 
     if (_validProperties.Values.Any(isValid => !isValid)) 
     { 
      AllPropertiesValid = false; 
      return; 
     } 
     AllPropertiesValid = true; 
    } 
    #endregion 

    #region Constructor 

    public ShellViewModel() 
    { 
     _user = new User(); 
     _validProperties = new Dictionary<string, bool> {{"Nick", false}, {"Password", false}}; 
    } 

    #endregion 

    #region Properties 

    public bool AllPropertiesValid 
    { 
     get { return _allPropertiesValid; } 
     set 
     { 
      if (_allPropertiesValid != value) 
      { 
       _allPropertiesValid = value; 
       NotifyOfPropertyChange("AllPropertiesValid"); 
      } 
     } 
    } 

    #endregion 

    #region Implementation of IShellViewModel 

    public string Nick 
    { 
     get { return _user.Nick; } 
     set 
     { 
      _user.Nick = value; 
      NotifyOfPropertyChange("Nick"); 
     } 
    } 

    public string Password 
    { 
     get { return _user.Password; } 
     set 
     { 
      _user.Password = value; 
      NotifyOfPropertyChange("Password"); 
     } 
    } 

    public void EmptyLogOn() 
    { 
     MessageBox.Show(string.Format("LogOn on server with credential: {0}, {1}", Nick, Password)); 
    } 

    public void LogOn(string nick, string password) 
    { 

     MessageBox.Show(string.Format("LogOn on server with credential: {0}, {1}", nick, password)); 
    } 

    #endregion 

    #region Implementation of IDataErrorInfo 

    public string Error 
    { 
     get { return (_user as IDataErrorInfo).Error; } 
    } 

    public string this[string propertyName] 
    { 
     get 
     { 
      string error = (_user as IDataErrorInfo)[propertyName]; 
      _validProperties[propertyName] = String.IsNullOrEmpty(error) ? true : false; 
      ValidateProperties(); 
      CommandManager.InvalidateRequerySuggested(); 
      return error; 
     } 
    } 

    #endregion 
} 

Wenn ich som Fehler habe ich gesetzt Eigenschaften auf falschen AllPropertiesValid. Ich binde diese Eigenschaften auf Button-Eigenschaften IsEnabled.

So in Aussicht Ich habe dies:

<Button IsEnabled="{Binding AllPropertiesValid, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
     Micro:Message.Attach="EmptyLogOn" 
     Content="Prihlás ma" 
     Width="100" 
     Height="25" 
     VerticalAlignment="Center" 
     Grid.Row="2" 
     Grid.ColumnSpan="2"></Button> 
<Label Content="{Binding AllPropertiesValid}" Grid.Row="3"/> 

Aber wenn Eigenschaften AllPropertiesValid falsche Taste ist immer noch aktiviert ist. Ich überprüfe den Wert von AllPropertiesValid (ich binde diese Eigenschaften auf Label und Label Inhalt ist falsch) ist falsch.

Was ist los? Danke für den Fortschritt.

BEARBEITEN: Im Designer ist Schaltfläche deaktiviert, aber wenn Fenster geladen ist Schaltfläche aktiviert ist.

Antwort

1

Wenn Sie tun MVVM dann sollten Sie ICommand (oder andere übergeordnete Varianten wie CommandBase, ...) verwenden, da Sie etwas tun müssen, wenn die Schaltfläche geklickt wird.

In diesem Fall binden Sie an eine Befehlseigenschaft im ViewModel, Sie geben false für CanExecute im Befehl zurück, und die Schaltfläche ist deaktiviert. Manchmal müssen Sie CommandManager.InvalidateRequerySuggested() anrufen.

Dies erklärt nicht, warum Ihr Code nicht funktioniert. Um ehrlich zu sein, sieht es für mich in Ordnung aus.

+0

Ich benutze Calibrn, weil ich Methode vom Service binde, ich würde nicht neuen Befehl erstellen, ich poste nur Beispiel. –