JohnB
JohnB

Reputation: 4379

How do I communicate events between ViewModels in a .Net WPF MVVM application?

I am creating a .Net WPF application that is a dashboard. I need the ability to communicate events between different View Models on the dashboard. This to me feels like a pub/sub events model.

What is the best way to implement a solution which fits well with the MVVM FrameWork? I started to look at Prism, but am wondering if that is a little heavy handed for my needs.

Can someone recommend a best practices approach and point me to some simple examples of implementation?

Thanks, JohnB

Upvotes: 2

Views: 1994

Answers (1)

user7583356
user7583356

Reputation: 76

for MVVM, i would definitely prefer to use MVVMLIGHTLIBS. First of all, there is an eventtrigger functionality, where you can do mousedown, mouseup, selectionchanged, etc.. it makes your life a lot easier when you try to bind a command for eventtrigger Example would be:

<i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
          <i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=employeeListBox, Path=SelectedValue}"/>
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </ListBox>

MyCommand will be implement in your viewmodel either using relaycommand or commandhandler, whichever you prefer. Also, knowledge regarding binding clr property, dependency property and attached property for controls are extremely important as well if you want to follow MVVM pattern. There are several examples that i would like to share: ---- event triggers--------- http://www.c-sharpcorner.com/blogs/example-of-eventtrigger-in-mvvm-application1 ----- attached property and dependency property---- To summarize: Attached properties are for the container elements. For example, you can have grid, and then you can create grid.rowdefinition and grid.columndefinition to have the number of row and column change dynamically

Dependency properties are properties of classes that derive from DependencyObject, and they're special in that rather than simply using a backing field to store their value, they use some helper methods on DependencyObject.

Those are pretty basic thing about wpf and mvvm, which involve model, view and viewmodel.

Upvotes: 0

Related Questions