Data Forms are found in just about every LOB application–can’t we make it easier?
Creating Line-of-Business (LOB) applications can become tedious, especially if you’re designing a data entry screen. Typically, you gather data from your users with a TextBlock, TextBox, CheckBox, RadioButton and for so forth. You begin by dragging those onto your designer, name the controls and position them where they look nice on the screen. Any additional data validation will need to come from code. If you left out a field, it's back to the designer, as you have to reposition your UI elements. I’m pleased to announce that your life just got a lot easier with DataForm in UI for Windows Universal.
Before we take a look at how we implement our DataForm control, let’s look at how we do it currently.
The MainPage.xaml for a typical data form may look like the following:
Which results in the following screen:
Each input field is given a name and some properties have been set. So far, we have seen a lot of XAML and are using a simple StackPanel to display the data properly.
What if you could define your UI through a class and have it generate your UI automatically? That's where the new DataForm comes into play.
After you have installed the Windows Software Development Kit (UWP), you will see the following templates:
Begin by using the Blank App (Universal Windows) template and adding references to the following files:
We are going to use the DataForm to automatically generate our UI in our app, so let’s begin by adding a class to this project to collect contact details. As you can see from the screenshot, the contact details on the first page include E-mail, First Name, Last Name, Title, Company and Phone Number. You can name the class Contact, and we will add the other fields later. The code contained in this file looks like this:
Our UI is simply going to reference the DataForm control. Let’s go ahead and add the proper XML namespace as shown below:
Add the following code snippet to the Grid already located on the MainPage.xaml.
We will need to add in the following code to set the Item property of the DataForm displayed on the screen:
Once complete, we see this:
As you can see, it automatically generated our UI based off the class that we instantiated. But, you may want to edit the field names as right now the “FirstName” needs a space between it (along with the other fields), or maybe you want to rearrange your UI to put the Company name after your last name. This is easily accomplished by adding the following property attribute to the class as shown below:
With this small change, we now have Labels with the correct spacing, and by using the Index property attribute, we changed our UI without going back inside our XAML file. We can finish this app up by adding a button and accessing the data the user entered with the click event handler.
Now our DataForm is starting to become alive! You could submit the data to a webserver, perform validation and so forth. I hope this helped; if you would like early access to a preview build of our Windows 10 controls, email email@example.com. Until next time, happy coding!
Michael Crump is a Microsoft MVP, Pluralsight and MSDN author as well as an international speaker. He works at Telerik with a focus on everything mobile. You can follow him on Twitter at @mbcrump or keep up with his various blogs by visiting his Telerik Blog or his Personal Blog.
Subscribe to be the first to get our expert-written articles and tutorials for developers!
All fields are required