2012-10-16 7 views
1

J'ai une zone de texte et un bouton. La commande de bouton doit modifier la propriété qui est liée au contrôle TextBox.Problème de liaison MVVM simple

Mais je ne vois aucun changement visuel après l'exécution de la commande. Je pense qu'il question facile lié à WPF contraignant

S'il vous plaît aidez-moi cette question

Source de l'application:

<UserControl.DataContext> 
    <local:SampleViewModel /> 
</UserControl.DataContext> 
<Grid> 
    <StackPanel> 
     <TextBox Height="23" Width="120" Text="{Binding MyName}" /> 
     <Button Content="Click" Command="{Binding ButtonCommand}" /> 
    </StackPanel> 
</Grid> 

ViewModel:

Private _myName As String 
Public Property MyName As String 
    Get 
    Return _myName 
    End Get 
    Set(value As String) 
    _myName = value 
     OnPropertyChanged("MyName") 
    End Set 
End Property 

Public _buttonCommand As DelegateCommand 
Public ReadOnly Property ButtonCommand As DelegateCommand 
Get 
    Return If(_buttonCommand IsNot Nothing, _buttonCommand, 
    New DelegateCommand(AddressOf Execute, AddressOf CanExecute)) 
End Get 
End Property 

Private Sub Execute() 
    MyName = "Executed" 
End Sub 

Private Function CanExecute() As Boolean 
    Return True 
End Function 

Public Event PropertyChanged As PropertyChangedEventHandler 
Private Sub OnPropertyChanged(propertyName As String) 
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) 
End Sub 
+0

Avez-vous essayé de mettre un point d'arrêt dans Execute()? – Blachshma

+0

pourriez-vous essayer de définir un point d'arrêt à 'Execute' et voir si elle est exécutée du tout? – Vlad

+0

Mon nom ne doit-il pas être observable? – IronMan84

Répondre

1

Est-ce que la suivant:

1. classe MainWindow Met en œuvre INotifyPropertyChanged

2. Dans Public Sub New() assurez-vous que vous écrivez Me.DataContext = Me pour régler le DataContext

Remarque: Ignorer l'étape 2 si vous utilisez un ViewModel et la mise en XAML

3. Modifier la ProperyChanged comme ceci: Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

seulement après implementing INotifyPropertyChanged correctly sera la liaison rafraîchir correctement la propriété MonNom après l'événement PropertyChanged

+0

Downvoter soin d'expliquer? – Blachshma

+0

Son code ne fonctionne pas et j'ai été en mesure de reproduire son problème sur VS 2010. Ce qui suit a résolu le problème sur mon ordinateur. – Blachshma

+0

Je ne peux pas reproduire son problème avec son VB et son XAML (VS12). Je me demande s'il a omis "Implements INotifyPropertyChanged" de la déclaration de classe. C'est la seule partie du code que j'ai supposé avoir fait. J'ai supprimé mon downvote sous l'hypothèse que l'OP a omis le code d'en-tête INPC. – user7116

1

Voici un code qui fonctionne avec votre XAML exact (je pris la mise en œuvre de DelegateCommand de http://wpftutorial.net/DelegateCommand.html) Désolé c'est C#, je ne suis vraiment pas dans VB: D

public class DelegateCommand : ICommand 
{ 
    private readonly Predicate<object> _canExecute; 
    private readonly Action<object> _execute; 

    public event EventHandler CanExecuteChanged; 

    public DelegateCommand(Action<object> execute) 
     : this(execute, null) 
    { 
    } 

    public DelegateCommand(Action<object> execute, 
        Predicate<object> canExecute) 
    { 
     _execute = execute; 
     _canExecute = canExecute; 
    } 

    public virtual bool CanExecute(object parameter) 
    { 
     if (_canExecute == null) 
     { 
      return true; 
     } 

     return _canExecute(parameter); 
    } 

    public virtual void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    public void RaiseCanExecuteChanged() 
    { 
     if (CanExecuteChanged != null) 
     { 
      CanExecuteChanged(this, EventArgs.Empty); 
     } 
    } 
} 

public class SampleViewModel : INotifyPropertyChanged 
{ 
    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    public DelegateCommand _buttonCommand; 
    public DelegateCommand ButtonCommand 
    { 
     get 
     { 
      if (_buttonCommand == null) 
      { 
       _buttonCommand = new DelegateCommand(Execute); 
      } 
      return _buttonCommand; 
     } 
    } 

    public void Execute(object o) 
    { 
     MyName = "executed"; 
    } 

    public string MyName { get { return _myName; } set { _myName = value; OnPropertyChanged("MyName"); } } 

    private string _myName; 
}