After finishing my last post, I saw I could improve it a little bit. The UI was too raw (my goal at the time was not to show the UI, but show how to add logging to a UWP app) and could be improved: instead of listviews, why not use a DataGrid to show the client's data?
Although there is no native DataGrid in the SDK, Telerik open sourced its UWP components, so you can use them in your UWP apps. If you want to know more, you can go to the Telerik's GitHub page (https://github.com/telerik/UI-For-UWP) and take a look at their components for UWP.
So, let's start where we left: you can go to my GitHub page (https://github.com/bsonnino/LoggingSerilog) and download the project developed in the last post. Then, add the NuGet package Telerik.UI.for.UniversalWindowsPlatform.
The first step in our change is to change the ListView in the Customers view to a RadDataGrid:
You should add the namespace Telerik.UI.Xaml.Controls.Grid in the beginning pf the XAML file:
With this simple change, you get a full featured Data Grid, that allows sorting, filtering or grouping. You can group by any column by dragging it to the group box, at the left:
Really nice, no? If you take a look at the grid, you will see that all the columns are being shown, including InDesignMode, a property introduced in ViewModelBase, but we don't want that. To set the columns, we have to set the property AutoGenerateColumns to False and set the columns we want in the Columns property. If you want, you can also set the CanUserChooseColumns to True, so the user can choose the columns he wants to display:
One extra twist is to add alternating columns to the grid. This is very easy, just set the AlternationStep property to a value greater than 1.
Now that we have the grid in place, let's go to the second step: use a DataForm for the detail view, That way you can have a single control for easy editing of objects.
Adding a DataForm to edit the selected item
The DataForm is an easy way to edit objects in UWP. With it, you don't need to add the editors for each field, you just need to add it and set the Item property to the item you want to edit:
As you can see, it works the same way as it did with all the TextBoxes, but the labels are not there. To fix this, we must add an attribute to the ViewModel properties to display the header:
The Display attribute will tell the form what is the label that must be shown and the placeholder to show in the edit box when it's empty. One note here is that the Display attribute that must be used isn't in System.ComponentModel.DataAnnotations, but it is in Telerik.Data.Core. You must add the correct namespace to use the Header and PlaceHolderText properties. Once you make these changes, the labels appear in the form:
Adding a chart to the view
Now that our program is working like the old one with the Telerik controls, let's enhance it with a Pie Chart that shows the percentage of customers by country. To do that, we must create a new property in the CustomersViewModel, CustomersByCountry. It will be initialized in the ViewModel's constructor:
We use LINQ to group the customers by country and generate the data ordered in descending order by the customer count. We have created a new class named CustomerCountry to store the total data for each country:
Once we have that in place, we can create our chart view. In the View folder, create a new UserControl and name it CustomersChart. In the chart, add this code:
We are adding a RadPieChart with a PieSeries in it. This PieSeries has the ItemsSource property set to the ViewModel's CustomersByCountry property. Its values are set to the NumCustomers property and the Legends are set to the country names. To add a legend, we must add a RadLegendControl and set its LegendProvider property bound to the chart. The ItemsPanel property is set to a ItemsWrapGrid, in a way that the items span to a new column if there is no available space at the bottom. The labels of the legend have the same color of the pie.
Now, we must add the new view to the main view. I've chosen to replace the log view with this new view, In MainPage.xaml, put this code:
When you run the app, you will see something like this:
Note: this chart is not dynamic - if you change the country for a customer or add a new customer, the chart doesn't update, because it's calculated in the constructor, To make it dynamic, you should recalculate it when the country of a customer has changed.
With these changes, we have an improved application, with a lot of new features.
Conclusions
As you can see, with small changes we got a lot of improvements for our app. With the DataGrid we gor sorting, filtering and grouping for our data with almost no effort. With the DataForm we can edit the customer without having to add TexBlocks and TextBoxes. If we change the underlying class, the form will be updated automatically. With the chart we could show a new visualization for our data. The Telerik controls for UWP are open source and free for you to use.
All the source code in this article is in https://github.com/bsonnino/TelerikGrid