Data Templates in Visual C#.NET

Drawing QR Code in Visual C#.NET Data Templates

Data Templates
Print QR In C#.NET
Using Barcode generator for VS .NET Control to generate, create QR-Code image in .NET framework applications.
www.OnBarcode.com
QR Code JIS X 0510 Recognizer In C#.NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
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.
Generate Barcode In Visual C#.NET
Using Barcode generator for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Code-128 Drawer In Visual C#.NET
Using Barcode generation for VS .NET Control to generate, create Code 128 Code Set C image in Visual Studio .NET applications.
www.OnBarcode.com
<ListBox x:Name="categoryList" DisplayMemberPath="DisplayName" SelectionChanged="categoryList_SelectionChanged">
QR Code JIS X 0510 Generation In Visual C#
Using Barcode printer for VS .NET Control to generate, create QR-Code image in Visual Studio .NET applications.
www.OnBarcode.com
Creating GTIN - 13 In C#
Using Barcode encoder for .NET Control to generate, create EAN13 image in Visual Studio .NET applications.
www.OnBarcode.com
Example 20-29 shows the code that puts the categories into it.
EAN 128 Printer In Visual C#
Using Barcode generation for .NET framework Control to generate, create EAN / UCC - 13 image in .NET framework applications.
www.OnBarcode.com
Drawing RM4SCC In C#.NET
Using Barcode creator for VS .NET Control to generate, create Royal Mail Barcode image in VS .NET applications.
www.OnBarcode.com
categoryList.ItemsSource = categoryViewModels;
Scanning QR In .NET Framework
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Creation In Java
Using Barcode printer for Java Control to generate, create QR image in Java applications.
www.OnBarcode.com
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 property, 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.
USS Code 128 Generation In None
Using Barcode drawer for Excel Control to generate, create Code-128 image in Excel applications.
www.OnBarcode.com
GS1 - 13 Recognizer In C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
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 showing 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 event of the category ListBox, which was hooked up in Example 20-28.
UCC - 12 Encoder In Java
Using Barcode maker for Java Control to generate, create UCC - 12 image in Java applications.
www.OnBarcode.com
UCC.EAN - 128 Generator In Objective-C
Using Barcode maker for iPad Control to generate, create GS1 128 image in iPad applications.
www.OnBarcode.com
private void categoryList_SelectionChanged(object sender, SelectionChangedEventArgs e) { CategoryViewModel currentCategory = categoryList.SelectedItem as CategoryViewModel; if (currentCategory == null) { productList.ItemsSource = null; } else { productList.ItemsSource = currentCategory.Products; } }
Printing UPC-A In None
Using Barcode creation for Excel Control to generate, create UPC Symbol image in Excel applications.
www.OnBarcode.com
Encode Barcode In Java
Using Barcode creator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
View Models and Details
Barcode Generation In VS .NET
Using Barcode generator for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
UPC A Encoder In Visual Basic .NET
Using Barcode creator for Visual Studio .NET Control to generate, create Universal Product Code version A image in .NET framework applications.
www.OnBarcode.com
While we don t want to distract you from the data binding details too much, there are a couple of points about the data sources in Example 20-30 that are worth being aware of. First, you ll have noticed the term view model cropping up in the names. This is a common name for a class that is not part of the view it contains no UI code but which is designed to be a data source for a particular view. We rarely data-bind directly to underlying domain model objects, because user interfaces usually introduce viewspecific state and logic that does not belong in the domain model. We want to be able to test this logic easily, so we don t want to bake it into the view code. We therefore add a layer between the view and the model, sometimes called the view model layer. You ll also sometimes see this approach described as separated presentation. Second, you might be wondering why the ListBox can t handle master/detail binding on its own, without us needing to add an event handler. Actually it can, but in this particular application, we don t necessarily have all the details up front we might want to fetch a product list for a category on demand, rather than making the user wait until the whole lot has been fetched before showing anything. In these situations, testing is often easier if you add explicit event handlers so that you know exactly when child data is going to be fetched. In the experience of the authors, supposedly clever code that implicitly relies on obscure tricks to get data binding to do the work in these situations is usually more trouble than it s worth.
Barcode Generation In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Code 128A Creator In None
Using Barcode maker for Font Control to generate, create Code 128A image in Font applications.
www.OnBarcode.com
This has some code to deal with the fact that we sometimes get a SelectionChanged event to notify us that nothing at all is selected. But the interesting code here looks much the same as before once again we re just setting the ItemsSource of a ListBox (the one on the right this time) to a collection of objects, the products in the selected category. Example 20-30 sets the ItemsSource in much the same way as Example 20-29, but the two listboxes on the left and right of Figure 20-16 look very different. That s because the Xaml for the second listbox is different:
<ListBox x:Name="productList" Grid.Column="1"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Path=DisplayName}" /> <Image Grid.Column="1" Source="{Binding Path=Thumbnail}" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Instead of using DisplayMemberPath to specify what text to display, this sets the ItemTemplate, which does for an items control s data items roughly what a control s Template property does for the whole control it defines the appearance. For each item in the ItemsSource, an instance of that DataTemplate will be created, with its DataContext set to the source item in question. So those two Binding expressions will pick up the Text property for the TextBlock and the Thumbnail property for the Image from the data source object for the product.
The fact that our source object provides a Thumbnail property is a good example of why we need a view model class that s distinct from the model. The underlying model may well offer the bitmap indeed, in this example, there is a model object (not shown, but available for download) with a property containing the raw binary data for the bitmap. And while WPF can automatically convert a byte array to the Image Source type the Image element requires, Silverlight cannot, and it becomes the job of the view model to transform the data into a suitable data type. So although the view model has no dependencies on the view code itself, it provides data tailored specifically for the view, even to the point of offering properties with types specific to WPF or Silverlight.
There is a connection between data templates and content controls: any content control is able to load a data template. (In fact, the heart of the mechanism is the ContentPre senter type that appears in any content control s template, as you saw in Example 20-20. This is the element that knows how to load a data template.) The reason items controls are able to instantiate a data template for each item is that the item containers (ListBoxItem, TreeViewItem, etc.) are content controls. So you can use data templates in all sorts of places for the content of buttons, the headers and contents of tab controls, the labels on tree views, and so on. Just as items controls offer an ItemTemplate property, you ll find similar ContentTemplate and HeaderTemplate properties that also accept data templates.
Summary
In this chapter, we discussed how you can build the structure of a user interface with Xaml, and how the associated code behind file can handle events and provide the UI elements with the information they need to perform their work. You saw some of the more important control types, and in particular, you looked at the content controls that can contain anything you like as content. You also saw how to connect your application s data to the screen with data binding.
Copyright © OnBarcode.com . All rights reserved.