WPF [SOLVED]: Pass data to parent view–communication between views

WPF [SOLVED]: Pass data to parent view–communication between views

Home Forums Frameworks WPF WPF [SOLVED]: Pass data to parent view–communication between views

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246874

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have created a view which has a GridView and a Button. I call it a parent view. At the beginning, the ItemsSource collection of GridView is empty. Now I click the button to pop up a modal/popup view I call it as a Child view.

    In the child view there are a couple buttons. One of them is to create new record for the parent view.

    For example, if I click it, a new row data is generated.

    Now my question is how to send the data just created back to the parent view so that the GridView in the parent view can be refreshed?

    #246875

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You can use IEventAggregator service to communicate between the two views Create an event, subscribe to it in the parent view or the parent view ViewModel, and publish it in the child view or the child view ViewModel. This way, there is no explicit dependency between the two views.
    Your record model class:

    public class RecordModel
    {
        //Some Properties
    }
    

    Declare the event:

    public class RecordAdded : PubSubEvent<RecordModel>
    {
    
    }
    

    In the parent view / viewModel:

    eventAggregator.GetEvent<RecordAdded>().Subscribe(OnRecordAdded);
    private void OnRecordAdded(RecordModel e)
    {
    
    }
    

    In your child view / viewmodel:

    eventAggregator.GetEvent<RecordAdded>().Publish(new RecordModel
    {
        //The popup data
    });
    

    Source: https://stackoverflow.com/questions/47954226/pass-data-to-parent-view-communication-between-views
    Author: Mahmoud
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.