Introduction
In this previous article we added a DomainDataSource control in our application and bounded it to a DataGrid and with their help we easily visualized our data. Now we are going to implement some more goodies into the application - sorting and filtering. We achieve that via the DomainDataSource control, as it provides such functionality and is really simple to use.
Here is a link to the live demo at this stage and the source code. Note that they will be updated with each article! ;)
Sorting
The DomainDataSource has a collection property called SortDescriptors. We can set it to a collection of descriptors and the DomainDataSource will sort the data when it gets loaded. The SortDescriptor class can be found in the System.Windows.Data in the System.Windows.Ria.Controls assembly. So if there isn’t one, add a reference to that assembly and declare the namespace in your XAML:
<navigation:Page x:Class="WebAdministrationTool.ManageUsers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...
xmlns:ria="clr-namespace:System.Windows.Controls;assembly=System.Windows.Ria.Controls"
xmlns:riaData="clr-namespace:System.Windows.Data;assembly=System.Windows.Ria.Controls"
...
Title="ManageUsers Page">
After that add a SortDescriptor to the DomainDataSource control:
<ria:DomainDataSource x:Name="UsersDataSource" LoadMethodName="LoadAspnet_Users">
<ria:DomainDataSource.SortDescriptors>
<riaData:SortDescriptor Direction="Ascending" PropertyPath="UserName" />
</ria:DomainDataSource.SortDescriptors>
...
</ria:DomainDataSource>
We want to sort the Data by UserName, so we set the propertyPath to that property’s name and choose the desired direction. I tried the following lines of code:
...
<ria:DomainDataSource.SortDescriptors>
<riaData:SortDescriptorCollection>
<riaData:SortDescriptor Direction="Ascending" PropertyPath="aspnet_Membership.IsApproved" />
<riaData:SortDescriptor Direction="Ascending" PropertyPath="UserName" />
</riaData:SortDescriptorCollection>
</ria:DomainDataSource.SortDescriptors>
...
with the idea to sort the users by the IsApproved flag first and after that by the name, but I got an error - “Unhandled Error in Silverlight 2 Application The value 'System.Windows.Data.SortDescriptorCollection' is not of type 'System.Windows.Data.SortDescriptor' and cannot be used in this generic collection”. Probably it will be available in the next releases or maybe there is another use of this SortDescriptorsCollection.
Filtering
The filtering allows us to take those items from data collection, which meet a certain requirement. Let’s say that we want to filter our data by the UserName. When typing in textbox, we want to be displayed those users whom UserName contains the typed string. In order to achieve this we use the FilterDescriptors property of the DomainDataSource control, but first let’s add a TextBox with the name “Filter”, that will allow the user to input the desired string. After that we add the FilterDescriptors:
<ria:DomainDataSource x:Name="UsersDataSource" LoadMethodName="LoadAspnet_Users">
...
<ria:DomainDataSource.FilterDescriptors>
<riaData:FilterDescriptorCollection>
<riaData:FilterDescriptor PropertyPath="UserName" Operator="Contains">
<riaData:ControlParameter ControlName="Filter" PropertyName="Text" RefreshEventName="TextChanged" />
</riaData:FilterDescriptor>
</riaData:FilterDescriptorCollection>
</ria:DomainDataSource.FilterDescriptors>
...
</ria:DomainDataSource>
Like the SortDescriptor control, the FilterDescriptor is located in the System.Windows.Ria.Controls assembly. We set the PropertyPath to the property we are filtering by and the Operator, which determines the pattern of filtering. The last can have the following values:
- Contains
- EndsWith
- IsContainedIn
- IsEqualTo
- IsGreaterThan
- IsGreaterThanOrEqualTo
- IsLessThan
- IsLessThanOrEqualTo
- IsNotEqualTo
- StartsWith
The last step is to add a ControlParameter to the FilterDescriptor, in order to give the control a value to filter with. The ControlName is for the name of the control to use, the PropertyName points to the property that should be used as filter and RefreshEventName controls when data to be filtered and refreshed.
Note: In this case we can use the FilterDescriptionCollection and add two FilterDescriptors, which use the same TextBox – one to filter by UserName and one by Email.
Also note that when you filter a new load of the data is performed and the data is filtered. This is very useful as if we have a DataPager with PageSize 10 and a DomainDataSource with LoadSize 10, we are still able to filter the whole data and not just the loaded. However a call to the DomainService is made, so this means we have to wait for the data to load.
Conclusion
Except to load simple and easy data, the DomainDataSource is also able to manage it. It can perform sorting and filtering and they don’t take much time and effort to implement. As a conclusion about the DomainDataSource I can say that this is one great control that allows us to easily load data from the server, sort and filter it and after that to provide it to the UI controls. One is for sure – it saved me a lot of code!