locked
MVVM-Viewmodel to viewmodel communication RRS feed

  • Question

  • Please let me know how can I communicate between 2 view models?

    I have a page with 2 user controls which contains 2 separate viewmodels.

    I want to bind the GridView of second user control viewmodel based on the value selected in first usercontrol viewmodel.

    How can I do that?

    Friday, April 27, 2012 7:21 AM

Answers

  • I think your question is more about a design pattern issue.

    Below disuccesion comes to a detailed explaination,hope this helps

    http://stackoverflow.com/questions/6392854/mvvm-pass-values-between-view-models

    Monday, April 30, 2012 3:37 AM
  • you can use MVVM Light Toolkit's Messaging Services to pass values to view Models.

    in My ViewModel One i have a property like this.

     public const string ImageInformationPropertyName = "ImageInformation";
            private Service.FileImageDetailInfo _ImageInformation = null;
            public Service.FileImageDetailInfo ImageInformation
            {
                get { return _ImageInformation; }
                set
                {
                    if (this._ImageInformation != value)
                    {
                        var oldValue = this.ImageInformation;
                        _ImageInformation = value;
                        base.RaisePropertyChanged(ImageInformationPropertyName, oldValue, this.ImageInformation, true);
                    }
                }
            }


    you see whenever value is changing i am broadcasting the changed value.

    PropertyName,OldValue,NewValue,IsBroadcasd

    ViewModel Two: how to get new Value

    Messenger.Default.Register<PropertyChangedMessage<Service.FileImageDetailInfo>>(this, info =>
                    {
                        if (this.PendingRedactionShapesCollection != null) { this.PendingRedactionShapesCollection.Clear(); }
                        var OldFileID = this.FileID;
                        this.ImageInformation = info.NewValue;
                        if (this.ImageInformation != null)
                        {    //only getting all Redaction Coordinates from database if FileID is not equal to old FileID
                            if (this.FileID != OldFileID)
                            {
                                //Getting saved Redaction Coordinates from database
                                this.GetFileImageRedactionsCoordinates();
                            }
                            else
                            {
                                //getting current image readciotn Corrdinates
                                this.GetCurrentImageCoordinates();
                            }
                        }
                    });
    Wednesday, May 2, 2012 7:04 AM

All replies

  • Have you thought about having all your context within one view model?  Your controls will use the page's vm.  You can still have control code behind handle the visualtree.  I believe that the idea behind mvvm is to have your data separate from your UI.  The VM is your data and the controls is your UI.

    If not you can look into messaging.  Or (I dont think this is a good idea) you could set a reference in one VM to the other one.

    Friday, April 27, 2012 10:16 AM
  • I think your question is more about a design pattern issue.

    Below disuccesion comes to a detailed explaination,hope this helps

    http://stackoverflow.com/questions/6392854/mvvm-pass-values-between-view-models

    Monday, April 30, 2012 3:37 AM
  • you can use MVVM Light Toolkit's Messaging Services to pass values to view Models.

    in My ViewModel One i have a property like this.

     public const string ImageInformationPropertyName = "ImageInformation";
            private Service.FileImageDetailInfo _ImageInformation = null;
            public Service.FileImageDetailInfo ImageInformation
            {
                get { return _ImageInformation; }
                set
                {
                    if (this._ImageInformation != value)
                    {
                        var oldValue = this.ImageInformation;
                        _ImageInformation = value;
                        base.RaisePropertyChanged(ImageInformationPropertyName, oldValue, this.ImageInformation, true);
                    }
                }
            }


    you see whenever value is changing i am broadcasting the changed value.

    PropertyName,OldValue,NewValue,IsBroadcasd

    ViewModel Two: how to get new Value

    Messenger.Default.Register<PropertyChangedMessage<Service.FileImageDetailInfo>>(this, info =>
                    {
                        if (this.PendingRedactionShapesCollection != null) { this.PendingRedactionShapesCollection.Clear(); }
                        var OldFileID = this.FileID;
                        this.ImageInformation = info.NewValue;
                        if (this.ImageInformation != null)
                        {    //only getting all Redaction Coordinates from database if FileID is not equal to old FileID
                            if (this.FileID != OldFileID)
                            {
                                //Getting saved Redaction Coordinates from database
                                this.GetFileImageRedactionsCoordinates();
                            }
                            else
                            {
                                //getting current image readciotn Corrdinates
                                this.GetCurrentImageCoordinates();
                            }
                        }
                    });
    Wednesday, May 2, 2012 7:04 AM
  • By creating the Reference object for the View Model_2's Model we can access the values of View Model_1's Model property.

    Tuesday, June 5, 2012 1:07 AM