WPF Empty ListView Template

When dealing with a ListView you want some kind a reusable code to show a message if there are no records using the MVVM pattern. First we'll create a style for the ListView in a resource dictionary and bind the control template to a property called Message that's going to live in the ViewModel. We can put this property in the BaseViewModel so you don't have to create it for each of your ViewModels. Below is the ListView style.

Next, we need to reference this style on the page or window where the ListView lives and set its style's DynamicResource to EmptyListViewStyle. Now it the ViewModel you can set the Message property to the appropriate message you wanna display when there is no data to show. Below is the code to get set up.

The ViewModel

The RoverProvider and Rover

Your ViewModelBase will implement INotifyPropertyChanged interface to start tracking properties.

15 views0 comments