Displaying your data in Visual Basic .NET

Making QR Code JIS X 0510 in Visual Basic .NET Displaying your data

12.2.1 Displaying your data
Quick Response Code Encoder In Visual Basic .NET
Using Barcode maker for Visual Studio .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
Denso QR Bar Code Reader In VB.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
The easiest thing to do with the DataForm is to bind it to an object and watch it generate all the fields you need. We ll step away from the Emoticon class for the remaining examples here to show the breadth of controls the DataForm understands. Let s create a new simple Person class, as shown in listing 12.5. (The Required attribute, used in this class, will be covered in chapter 13, which focuses on validation.)
Creating Code 128C In VB.NET
Using Barcode drawer for VS .NET Control to generate, create Code 128 Code Set A image in VS .NET applications.
www.OnBarcode.com
Barcode Generation In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Listing 12.5 The Person class
GTIN - 13 Drawer In Visual Basic .NET
Using Barcode encoder for .NET Control to generate, create GS1 - 13 image in VS .NET applications.
www.OnBarcode.com
Generate DataMatrix In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
public enum MaritalStatus { Unknown, Married, Single, Divorced } public class Person { [Required]
QR Code Generation In VB.NET
Using Barcode maker for VS .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
GTIN - 14 Creator In Visual Basic .NET
Using Barcode generation for VS .NET Control to generate, create Case Code image in .NET framework applications.
www.OnBarcode.com
Download from Wow! eBook <www.wowebook.com>
Draw Quick Response Code In Objective-C
Using Barcode creator for iPhone Control to generate, create QR image in iPhone applications.
www.OnBarcode.com
Decode Quick Response Code In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
The DataForm
PDF-417 2d Barcode Recognizer In .NET Framework
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Draw Barcode In Objective-C
Using Barcode generation for iPad Control to generate, create Barcode image in iPad applications.
www.OnBarcode.com
public string LastName { get; set; } [Required] public string FirstName { get; set; } public bool IsRegistered { get; set; } public MaritalStatus MaritalStatus { get; set; } public DateTime DateOfBirth { get; set; } [Required] public string EmailAddress { get; set; } [Required] public int NumberOfChildren { get; set; } }
Paint PDF-417 2d Barcode In None
Using Barcode generator for Software Control to generate, create PDF-417 2d barcode image in Software applications.
www.OnBarcode.com
Data Matrix ECC200 Printer In VS .NET
Using Barcode generator for VS .NET Control to generate, create ECC200 image in VS .NET applications.
www.OnBarcode.com
We ll then bind to it in XAML using a static resource, much as we have in other examples. Instead of individual controls, we ll use the entire object as our data source for the new DataForm control, as shown in listing 12.6.
PDF 417 Generator In None
Using Barcode encoder for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
GS1 RSS Generator In VS .NET
Using Barcode printer for .NET framework Control to generate, create GS1 DataBar Limited image in .NET framework applications.
www.OnBarcode.com
Listing 12.6 Binding the DataForm to a single Person object
Scan UPC Symbol In C#
Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
EAN 128 Recognizer In Visual C#.NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
<UserControl.Resources> <local:Person x:Key="me" /> </UserControl.Resources>
Draw Barcode In Java
Using Barcode generation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Barcode Recognizer In VS .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Person as resource Binding to person
<Grid x:Name="LayoutRoot" Margin="30"> <toolkit:DataForm CurrentItem="{StaticResource me}" /> </Grid>
The property that s used to generate the form is the CurrentItem property, in this sample case bound to a single object sitting in the Resources section of this control. The resulting DataForm, as seen in figure 12.1, is impressive in the breadth of controls it has auto-generated for you. Not only did we not have to write any code specific to the DataForm, but we also didn t have to place any edit controls in the markup. Note that the DataForm displayed a CheckBox for the bool property, a ComboBox for the enum (populated with all the possible values defined in the enumeration, of course), and a DatePicker for the DateTime property. Not bad for a default form, and certainly workable for a simple utility application.
Figure 12.1 DataForm showing generated edit controls bound to a single object without a backing collection. Note that there s no toolbar or set of navigation buttons. Not all fields are shown, so your form will look slightly different.
Download from Wow! eBook <www.wowebook.com>
Data controls: DataGrid and DataForm
So far we ve been binding one discrete object to the DataForm. To harness the true power of the DataForm, you ll want to bind it to a list of objects much as you would a DataGrid.
12.2.2 Binding to lists of data
If you want to support the ability to add new records, you ll need to provide a place to put them. For this example, we ll create a simple class that holds some dummy data. Of course, you could wire this up to a service to load a collection of Person objects, should you desire. Listing 12.7 shows a class named PeopleRepository that will hold our Person objects. Note that this doesn t follow the formal Repository pattern; it s closer to a View-Model (covered in chapter 16).
Listing 12.7 The PeopleRepository class
public class PeopleRepository { private ObservableCollection<Person> _people = new ObservableCollection<Person>(); public ObservableCollection<Person> People { get { return _people; } } public PeopleRepository() { _people.Add(new Person() { FirstName = "Captain", LastName = "Avatar", IsRegistered = true, MaritalStatus = MaritalStatus.Unknown, DateOfBirth = DateTime.Parse("1912-01-01") }); _people.Add(new Person() { FirstName = "Derek", LastName = "Wildstar", IsRegistered = true, MaritalStatus = MaritalStatus.Single, DateOfBirth = DateTime.Parse("1954-11-15") }); } }
Observable collection of Person
Load dummy data
Once you have a suitable repository for the data (whether it s a view-model or something else), one of the easiest things to do is to supply an ObservableCollection<T> to the ItemsSource property, as shown in listing 12.8.
Listing 12.8 Binding the DataForm to the PeopleRepository class
<UserControl.Resources> <local:PeopleRepository x:Key="repository" /> </UserControl.Resources>
Repository as resource
Download from Wow! eBook <www.wowebook.com>
The DataForm
<Grid x:Name="LayoutRoot" Margin="30"> <toolkit:DataForm DataContext="{StaticResource repository}" ItemsSource="{Binding People}" CurrentIndex="0"> </toolkit:DataForm> </Grid>
Repository reference Observable collection
In the example, I use the PeopleRepository class from listing 12.7 with a collection of Person objects exposed through a property named People. In XAML, I create a resource to hold a reference to that repository and set the DataContext of the DataForm to that StaticResource. (You could, of course, also create the repository and set the DataContext from code.) I then bound the ItemsSource to the collection of Person objects. The resulting DataForm looks like figure 12.2.
Figure 12.2 A DataForm bound to a collection of objects. Note the presence of the toolbar including the add/remove and navigation buttons, sometimes called the VCR control. (Display annotations, covered in 12.3.1, were used in this example.)
Note the new toolbar at the top of the DataForm. This provides navigation as well as Add (the plus sign) and Delete (the minus sign) capabilities. For each of the operations (Add, Delete, Validate, and so on) appropriate events are raised with the capability to cancel operations based on criteria you set in your code.
Copyright © OnBarcode.com . All rights reserved.