2010-03-15 20 views
19

नहीं बुलाया जा रहा है मैं दो बक्सें (उपयोगकर्ता नाम और पासवर्ड) और एक लॉग इन बटन के साथ एक सरल लॉगिन UserControl कोडिंग कर रहा हूँ। मैं लॉगिन बटन केवल तभी सक्षम होना चाहता हूं जब उपयोगकर्ता नाम और पासवर्ड फ़ील्ड भर जाए। मैं प्रिज्म और एमवीवीएम का उपयोग कर रहा हूं। LoginViewModel में एक प्रोफ़ाइल है जिसे LoginCommand कहा जाता है जो लॉगिन बटन से जुड़ा हुआ है। मैं अपने ViewModel में एक CanLoginExecute() विधि है, लेकिन यह आग केवल जब आवेदन कभी नहीं फिर से और फिर आता है। तो लॉगिन बटन कभी सक्षम नहीं है। मैं क्या खो रहा हूँ?WPF-प्रिज्म CanExecute विधि

<TextBox x:Name="username" 
    Text="{Binding Path=Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" /> 
<TextBox x:Name="password" 
    Text="{Binding Path=Password, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" /> 
<Button Content="Login" 
    cmnd:Click.Command="{Binding LoginCommand}" /> 

यहाँ मेरी ViewModel

class LoginViewModel : IDataErrorInfo, INotifyPropertyChanged 
{ 
    public LoginViewModel() 
    { 
     this.LoginCommand = 
      new DelegateCommand<object>(
       this.LoginExecute, this.CanLoginExecute); 
    } 

    private Boolean CanLoginExecute(object dummyObject) 
    { 
     return (string.IsNullOrEmpty(Username) || 
       string.IsNullOrEmpty(Password)) ? false : true; 
    } 

    private void LoginExecute(object dummyObject) 
    { 
     if (CheckCredentials(Username, Password)) 
     { 
      .... 
     } 
    } 

    #region IDataErrorInfo Members 

    public string Error 
    { 
     get { throw new NotImplementedException(); } 
    } 

    public string this[string columnName] 
    { 
     get 
     { 
      string result = null; 
      if (columnName == "Username") 
      { 
       if (string.IsNullOrEmpty(Username)) 
        result = "Please enter a username"; 
      } 
      else if (columnName == "Password") 
      { 
       if (string.IsNullOrEmpty(Password)) 
        result = "Please enter a password"; 
      } 
      return result; 
     } 
    } 

    #endregion // IDataErrorInfo Members 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    #endregion // INotifyPropertyChanged Members 

    #region Properties 

    private String _username; 
    public String Username 
    { 
     get { return _username; } 
     set 
     { 
      if (value == _username) 
       return; 
      _username = value; 
      this.OnPropertyChanged("Username"); 
     } 
    } 

    private String _password; 
    public String Password 
    { 
     get { return _password; } 
     set 
     { 
      if (value == _password) 
       return; 
      _password = value; 
      this.OnPropertyChanged("Password"); 
     } 
    } 

    public ICommand LoginCommand { get; private set; } 

    #endregion // Properties 
} 
+0

क्या cmnd है: Click.Command =, यह कुछ प्रिज्म विशिष्ट है। मैं आमतौर पर <बटन कमांड = "{बाइंडिंग लॉगिन कॉमांड}" करता हूं और मेरे लिए काम करता है। जब से मैं उपयोग कर रहा हूँ ; –

+0

हाँ, cmnd: xmlns:: cmnd = "विधानसभा = Microsoft.Practices.Composite.Presentation Microsoft.Practices.Composite.Presentation.Commands clr-नाम स्थान" Click.Command = चश्मे विशिष्ट है प्रिज्म के प्रतिनिधिमंडल, मैंने सोचा कि यह संगत कमांड बाध्यकारी तंत्र होगा। मैंने सीधे कमांड = "{बाइंडिंग लॉग इन कॉमांड}" भी कोशिश की है - यह बिल्कुल वही काम करता है। – Naresh

उत्तर

40

ऐसा नहीं है कि बाध्य नियंत्रण फिर कभी नहीं CanExecute राज्य के लिए पूछ रहा है सबसे अधिक संभावना है:

यहाँ मेरी XAML है। आप कॉल करने के लिए RaiseCanExecuteChanged DelegateCommand पर विधि जब भी आप एक शर्त है कि आदेश के CanExecute राज्य में परिवर्तन का पता लगाने की जरूरत है। यह CanExecute राज्य अद्यतन करने के लिए बाध्य नियंत्रण संकेत देती है।

+0

RaiseCanExecuteChanged एक आकर्षण की तरह काम करता है! धन्यवाद ओली – Naresh

+18

बस रिकार्ड के लिए (यह मेरे लिए हो रहा था), मैं 'RaiseCanExecuteChanged' विधि नहीं मिल रहा था क्योंकि मैं ICommand इंटरफ़ेस का उपयोग किया गया था। इस विधि को DelegateCommand कार्यान्वयन में परिभाषित किया गया है, इसलिए मुझे इसे डालने की आवश्यकता है। – alf

8
RaiseCanExecuteChanged के लिए

कोड:

private void RaiseCanExecuteChanged() 
    { 
     DelegateCommand<object> command = LoginCommand as DelegateCommand<object>; 
     command.RaiseCanExecuteChanged(); 
    } 

    public const string UsernameProperty = "Username"; 
    private String _username; 
    public String Username 
    { 
     get { return _username; } 
     set 
     { 
      _username = value; 
      this.NotifyPropertyChanged(UsernameProperty); 
      RaiseCanExecuteChanged(); 
     } 
    } 
संबंधित मुद्दे