Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM-Light Toolkit -- How To Use PropertyChangedMessage

Tags:

wpf

mvvm-light

Can someone please post a working example of the PropertyChangedMessage being used? The description from the GalaSoft site states:

PropertyChangedMessage: Used to broadcast that a property changed in the sender. Fulfills the same purpose than the PropertyChanged event, but in a less tight way.

However, this doesn't seem to work:

private bool m_value = false;
public bool Value
{
    get { return m_value ; }
    set 
    { 
        m_value = value;
        Messenger.Default.Send(new PropertyChangedMessage<bool>(m_value, true, "Value"));
    }
like image 625
Big Tuna Avatar asked Dec 16 '25 22:12

Big Tuna


1 Answers

This is related with the MVVM Light Messenger.

In your property definition yo use like this:

public string Name {
    get
    {
        return _name;
    }
     set
    {
        if (_name == value)
        {
            return;
        }
         var oldValue = _name;
        _name = value;
         // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
        RaisePropertyChanged(Name, oldValue, value, true);
    }
}

Then you can suscribe to any modification on the property using something like this:

Messenger.Default.Register<PropertyChangedMessage<string>>(
    this, (e) => this.Name = e.NewValue
);

Look at this post and read about the MVVM Light Messenger

To broadcast:

Messenger.Default.Send<PropertyChangedMessage<string>>(oldValue, newValue, "PropertyName");
like image 68
Salvador Sarpi Avatar answered Dec 20 '25 02:12

Salvador Sarpi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!