2010-12-11 34 views
0

ビューでのエラー処理に問題があります。私はcaliburn.microとMEFを使用します。WPF-MVVMでのエラー処理/無効化ボタン

私のVMのようになります。私はソムエラーを持っている

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

なら、私は偽にAllPropertiesValidプロパティを設定します。私はこのプロパティをButtonプロパティIsEnabledにバインドします。

だから、ビューに私はこれがあります。

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

をしかし、プロパティAllPropertiesValidはまだ有効になっている偽のボタンである場合。私はAllPropertiesValidの値をチェックします(私はこのプロパティをラベルにバインドし、ラベルの内容はfalseです)はfalseです。

どういうところが間違っていますか?事前に感謝します。

EDIT:デザイナーではボタンが無効になっていますが、ロードされているときにウィンドウボタンが有効になっています。

答えて

1

あなたはボタンがクリックされたときに何かをする必要があるので、あなたは、あなたがICommand(または、CommandBaseなどの他のより高いレベルの変種、...)を使用する必要がありますMVVMを行っている場合。

この場合、ViewModelのコマンドプロパティにバインドし、コマンドのCanExecuteでfalseを返し、ボタンが無効になります。場合によってはCommandManager.InvalidateRequerySuggested()に電話する必要があります。

これは、コードが機能しない理由を説明していません。正直言って、それは私に大丈夫だ。

+0

私はサービスからメソッドをバインドするので、私はcaliburnを使用します。私は新しいコマンドを作成しません。 –

関連する問題