Learning how to communicate again
As we tend not to handle UI events directly, when using MVVM, we need alternative ways to implement the same functionality that they provide. Different methods are required to reproduce the functionality of different events. For example, the functionality of the SelectionChanged
event of a collection control is typically reproduced by data binding a View Model property to the SelectedItem
property of the collection control.
<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding CurrentItem}" />
In this example, the setter of the CurrentItem
property will get called by the WPF Framework each time a new item is selected from the ListBox
. Therefore, instead of handling the SelectionChanged
event in the code ...
Get Mastering Windows Presentation Foundation now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.