latata
latata

Reputation: 1723

Communication between two separated views in WPF

I have MainWindows.xaml containing two Controls elements. First is connected with MapView.xaml <Controls:MapView /> and second with InfoView.xaml <Controls:InfoView />. Now I want to execute method from InfoView dataContext object InfoViewModel from MapView DataContext Object MapViewModel.

How can I do this?

MapView.xaml.cs

public MapView()
    {
        InitializeComponent();
        _mapViewModel = new MapViewModel();
        this.DataContext = _mapViewModel;
    }

InfoView.xaml.cs

public InfoView()
    {
        InitializeComponent();
        _infoViewModel = new InfoViewModel();
        this.DataContext = _infoViewModel;
    }

Upvotes: 0

Views: 114

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

One of approacges is to use the EventAggregator and your own event to send the notification from the source and catch it in the subscriber.

An advantage of this approach is that is it generic, you can send any messages from anywhere to anywhere to accomplish anything, you send the message from the source and catch it in the subscriber.

The EventAggregator is built into Prism.

Upvotes: 1

Related Questions