In this short post I will show you how you can bind the list view control and get the selected value of the list view control using MVVM pattern. For this post I have added one list view control which hold the grid view control in it view and when item is selected in the grid view the selected item is shown at the end of the screen. In the Image 1the selected items are not shown ( only the label fields are shown) but in the Image 2 when user click on any of the item the detail is shown.
Here you can see the first name and last name are concatenated to make the full name, email address, contact number and the date of birth of the selected person is shown.
In the model folder I have one file with the name MainWindowModel which has one class with the name of the Persons. The Persons class has the UserID, first name, last name, EmailID , contact number and the date of birth properties.
In the viewModel folder I have one file with the name MainWindowViewModel which contain class of same name. The viewModel has two properties one is the PersonRecords ( which is list of Person records used to bind with the list view control) and the second one is the SelectedPersonRecord( which is of type Person and used to bind with the selected value of the list view control.which holds the selected value of the list view control).
In the view , I have list view control which is bind to the PersonRecords ( the ItemSource of the list view control) and the SelectedItem of the list view control is bind to the SelectedPersonRecord as you can see in the code below and the binding mode of the SelectedItem is two way as I want to send back the latest changes to the viewModel as well.
When user click on any of the item in the list view the corresponded value is shown at the bottom of the form. which has dataContaxt of SelectedRecord. I have set the dataContext of each of the textblock control to the SelectedPersonRecord property and then use the Run object of the Textblock control to format and bind to the properties like first name, last name, emailID, contact number and date of birth.You can download the source code from here
All and any comments / bugs / suggestions are welcomed!