3

I have the following view model

[NotifyPropertyChanged]
public class ActivateViewModel
{
    public string Password { get; set; }
    public bool ActivateButtonEnabled { get { return !string.IsNullOrEmpty(Password); } }
    ...
}

In my view I'm trying to enable/disable a button depending on whether a password textbox has a value or not.

ActivateButtonEnabled is not being notified automatically when the Password property changes. What am I doing wrong? I was reading this article and if I understand correctly PostSharp should be able to handle dependent properties automagically.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • 1
    This should work with PS out of the box. Please, could you post your xaml here? What kind of project are you using (wpf, silverlight, WP, etc...)? – Jakub Linhart Feb 27 '15 at 11:51

3 Answers3

0

I think you need to access the password as 'this.Password' as PostSharp expects the 'this' accessor before all dependant properties.

Phil Wright
  • 22,580
  • 14
  • 83
  • 137
0

Please consider using the ICommand interface. The interface contains ICommand.CanExecute Method that determines whether the command can execute in its current state. An instance of ICommand interface can be bound to the Command property of the Button instance. If the command can not be executed, the button will be disabled automatically.

The ICommand interface implementation which has RaiseCanExecuteChanged()-like method must be used to achieve the described behavior, for example:

  • DelegateCommand class from Prism library.
  • RelayCommand from MVVM Light library.
  • etc.

The implementation of ViewModel using DelegateCommand class from Prism library:

[NotifyPropertyChanged]
public class ActivateViewModel
{
    private readonly DelegateCommand activateCommand;
    private string password;

    public ActivateViewModel()
    {
        activateCommand = new DelegateCommand(Activate, () => !string.IsNullOrEmpty(Password));
    }

    public string Password
    {
        get { return password; }
        set
        {
            password = value;
            activateCommand.RaiseCanExecuteChanged(); // To re-evaluate CanExecute.
        }
    }

    public ICommand ActivateCommand
    {
        get { return activateCommand; }
    }

    private void Activate()
    {
        // ...
    }
}

XAML-code:

<Button Content="Activate"
        Command="{Binding ActivateCommand}" />

Have not found the documentation about PostSharp's ICommand-interface support, but a question: INotifyPropertyChanged working with ICommand?, PostSharp Support.

  • I appreciate the effort, but it seems silly to pull in prism and go out of my way for something that should work out of the box with the tools that I currently have. – The Muffin Man Feb 24 '15 at 16:01
  • @TheMuffinMan, sure, Prism library is used just for example. Another suitable `ICommand` interface implementation can be used (other libraries) or created in the current solution (project). The answer has been updated. – Sergey Vyacheslavovich Brunov Feb 24 '15 at 18:22
0

In the view, what control are you using? a PasswordBox? is posible that the property password is never updated.

The Passwordbox.Password is not a dependency property for security reasons, and then, not supports bindings. You have an explanation and a possible solution in:

http://www.wpftutorial.net/PasswordBox.html

If the control is not a passwordbox, can you write us the view?

  • I was using an event handler for passwordchanged event and then manually setting `Password` within the handler, but I also tried using a regular textbox without the changed handler and it still didn't work. – The Muffin Man Feb 24 '15 at 15:54
  • The view is simple. `Textbox text="{Binding Path=Password}"` and `Button IsEnabled="{Binding Path=ActivateEnabled}"` – The Muffin Man Feb 24 '15 at 16:04