Address: http://jesseliberty.com/2011/01/07/windows-phone-from-scratch-19-mvvm-light-toolkit-soup-to-nuts-4/
Let's review that the first three parts use viewmodel and bind it to viewmodel. In this mini-tutorial, I will show you how to bind the collection in viewmodel To The ListBox in the view. Next, I will show you how to capture the choice of ListBox and how to display the details page in viewmodel.
To create a simple application, we will display the full name of the customer and their email, and finally allow the user to click a customer and display the details of the customer.
We started to create a new mvvm program. After creation, open in blend and add a ListBox in the content panel.
Create customercollection class
The customercollection class provides some data for display. I borrowed this class from the previous demo. This class contains many fields (surname, name, address, city, zip code, phone number, fax, email), attributes, and most importantly, returns an observable collection with random mixing and matching values.
Binding the itemsource
The key to understanding how mvvm processes data binding is that the datacontext of view is viewmodel. As you expected, the itemsource binding in the view should be as follows:
1: <ListBox
2: x:Name="PersonListBox"
3: Margin="10"
4:
Itemssource = "{binding MERs}">
When mvvm light is installed, the data context on the page should be set to viewmodel. You need a read-only attribute to bind it. I suggest using code snippet in mvvm light.
Therefore, return vs. In the code behind file and mainviewmodel. CS, enter mvvminpc and press tab. This will help you quickly complete the task of entering attributes. You can set the attribute name to mers MERs and return the variable to _ customers. However, we do not need to return variables or setter, so we will use the MERs attribute in the customercollection class.
1: public ObservableCollection<Customer> Customers
2: {
3: get
4: {
5: var customerCollection = new CustomerCollection();
6:
Return customercollection. customers;
7: }
8: }
The rest is to provide a datatemplate in the view, so that each customer is displayed as we want:
1: <ListBox
2: x:Name="PersonListBox"
3: Margin="10"
4: ItemsSource="{Binding Customers}">
5: <ListBox.ItemTemplate>
6: <DataTemplate>
7: <StackPanel>
8: <StackPanel
9: x:Name="DataTemplateStackPanel"
10: Orientation="Horizontal">
11: <TextBlock
12: x:Name="FirstName"
13: Text="{Binding First}"
14: Margin="0,0,5,0"
15: Style="{StaticResource PhoneTextExtraLargeStyle}" />
16: <TextBlock
17: x:Name="LastName"
18: Text="{Binding Last}"
19: Margin="0"
20: Style="{StaticResource PhoneTextExtraLargeStyle}" />
21: </StackPanel>
22: <TextBlock
23: x:Name="Email"
24: Text="{Binding Email}"
25: Margin="0"
26: Style="{StaticResource PhoneTextSubtleStyle}" />
27:
28: </StackPanel>
29: </DataTemplate>
30: </ListBox.ItemTemplate>
31: </ListBox>
Finally, we need to process the events that users click on each customer and go to a details page. The next part will continue to be discussed.
Source code download