none
How to use a delegate method between view models with MVVM

    Question

  • I am looking for some advice for the following scenario.

    1. I have a view model property that is bound (Two-Way) to a view value.
    2. When the property changes, I would like to update a different view model property value so that when its view is displayed it has the latest value.

    I don’t want to use any add-ons to implement this functionality. What are the preferred way(s) to achieve this using a delegate method for the MVVM pattern? If you have an example, I would appreciate your sharing it.

    I am using INotifyPropertyChanged and INotifyDataErrorInfo for communicating changes between the view models and views. The base class for change notifications is NOT static, which seems to create an issue when trying to use delegates between two or more bound properties that use a non-static instance of INotifyPropertyChanged and INotifyDataErrorInfo.

    I am looking for a good way to achieve this communication while preserving loose coupling between the different view model properties.

    Thanks,

    Warren

    Saturday, November 12, 2011 11:04 AM

Answers

  • If you have a view model that has a direct coupling with another view model (which is implied from the properties) you would simply have view model 2 register to property change notification from view model 1 and if the property changes, update it. Otherwise, if the data is truly common, create an instance of that data shared by both view models and when it updates, it will reflect. Finally, if you want local properties and completely decoupled view models, the change of the property should fire a message. The second view model can subscribe to the message and update to the property when it changes.

    Saturday, November 12, 2011 1:08 PM
  • Hi Warren

    I use the last method that Jeremy has already mentioned.  I use the eventaggregator from Prism, if you haven't looked into Prism yet it might be worth a look as you pull out the bits that you need.

    http://compositewpf.codeplex.com/

    Paul

     

    Tuesday, November 15, 2011 9:24 AM

All replies

  • The following approach works with public static pointers for the second class, but I am hoping to eliminate this type of coupling, if possible.

    Public delegate method…

    public delegate void SetStringValue(string stringValue);

    Class1 View Model Property…

    private string propertyName = null;

    public string PropertyName

    {

           get { return propertyName; }

           set

           {

                  if (propertyName!= value)

                  {

                         if (Class2.class2VM == null)

                               Class2.class2 = new Class2();

                         SetStringValue ssv = new SetStringValue(Class2.class2VM.SetPropertyValue);

                         ssv(value);

                         propertyName = value;

                         RaisePropertyChanged("PropertyName");

                  }

           }

    }

    Class2…

    public static Class2ViewModel class2VM;

    public static Class2 class2;

    I created public static variables for second class. Is there a better way to do this???

    Class2 View Model…

    private string propertyName = null;

    public string PropertyName

    {

           get { return propertyName; }

           set

           {

                  if (propertyName!= value)

                  {

                         propertyName = value;

                         RaisePropertyChanged("PropertyName");

                  }

           }

    }

    //This method is called through the delegate to set the second class property

    public void SetPropertyValue(string value)

    {

           PropertyName = value;

    }

     

    Saturday, November 12, 2011 12:22 PM
  • If you have a view model that has a direct coupling with another view model (which is implied from the properties) you would simply have view model 2 register to property change notification from view model 1 and if the property changes, update it. Otherwise, if the data is truly common, create an instance of that data shared by both view models and when it updates, it will reflect. Finally, if you want local properties and completely decoupled view models, the change of the property should fire a message. The second view model can subscribe to the message and update to the property when it changes.

    Saturday, November 12, 2011 1:08 PM
  • Thanks Jeremy,

    Do you happen to have any Silverlight examples or links to examples that illustate the three methods you described above? Conceptually, I understand what you said, but could use some examples to study and build upon.

    Warren

    Saturday, November 12, 2011 1:25 PM
  • Hi Warren

    I use the last method that Jeremy has already mentioned.  I use the eventaggregator from Prism, if you haven't looked into Prism yet it might be worth a look as you pull out the bits that you need.

    http://compositewpf.codeplex.com/

    Paul

     

    Tuesday, November 15, 2011 9:24 AM