Uses just a couple of ad hoc binding expressions in a

Info icon This preview shows pages 793–796. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 20-25 uses just a couple of ad hoc binding expressions in a user interface, but there’s a slightly more structured and very powerful data binding feature you can use with item controls: data templates. Data Templates Just as a control’s appearance is defined by a control template, you can create a data template to define the appearance of a particular data type. Look at the user interface in Figure 20-16 —it shows a pair of listboxes, in a typical master/details scenario. The ListBox on the left looks fairly ordinary—it lists product categories, showing each one as simple text. You might think this works by fetching the list of categories and then iterating over them with a loop that creates a ListBoxItem for each one. In fact, it’s much simpler than that. Example 20-28 shows the Xaml for the ListBox on the left. Data Binding | 769
Image of page 793

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This application is using the Adventure Works sample database intro- duced in Chapter 14 , which the hosting web application is making available to the Silverlight client with a combination of the WCF Data Services mechanism described in the same chapter, and some of the networking features described in Chapter 13 . The precise details of the server code are not directly relevant to this chapter, but you can get the code by downloading the samples from this book’s web page: http:// oreilly.com/catalog/9780596159832/ . Example 20-28. ListBox displaying simple text <ListBox x:Name="categoryList" DisplayMemberPath="DisplayName" SelectionChanged="categoryList_SelectionChanged"> Example 20-29 shows the code that puts the categories into it. Example 20-29. Providing items for a listbox categoryList.ItemsSource = categoryViewModels; Obviously, we left out some code—that categoryViewModels variable, which contains a list of objects each representing a category, had to come from somewhere. But right now we’re focusing on how the data gets hooked up to the UI, not where it came from, so to avoid distracting you with details irrelevant to this chapter’s topic, we’re just showing the code that deals with the UI aspects. And as you can see, it’s really very simple. ListBox derives from ItemsControl , from which it inherits an ItemsSource prop- erty, and you can assign any collection into ItemsSource . The control will iterate through the collection for you, generating an item container (a ListBoxItem in this case) for every object. Figure 20-16. Lists with data templates 770 | Chapter 20: WPF and Silverlight
Image of page 794
The Xaml sets the DisplayMemberPath attribute to DisplayName —this determines which property on the source object the ListBoxItem reads to work out what text to display for the object. And that’s why the lefthand list displays the category names. But clearly the list on the righthand side of Figure 20-16 is much more interesting. It shows all the products for the currently selected category, but it’s not just displaying text—it’s show- ing an image for each product. The product list is updated when we select a category, and Example 20-30 shows the code that handles the SelectionChanged
Image of page 795

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 796
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern