HomeSilverlight Silverlight 4.0: Navigating Data Using Dom...
Silverlight 4.0: Navigating Data Using DomainDataSource
This article gives you a great introduction to navigating through the objects available in the “DomainDataSource” control using Silverlight (with WCF RIA Services).
To make this article simple, I built a simple Silverlight 4.0 application which consumes a WCF RIA Service created using the WCF RIA Service Library. If you are not familiar with developing applications using the WCF RIA Service Library and Silverlight 4.0, check out a beginner’s article.
The entire source code for this article is available in the form of a free downloadable zip file. The solution was developed using Microsoft Visual Studio 2010 Ultimate Edition with Microsoft Silverlight 4.0 on Windows 7 Ultimate edition. I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems with execution.
Navigating through data using DomainDataSource: design
In my previous articles, I explained the importance of DomainDataSource and how we can query, sort, group and filter data using the same in a Silverlight 4.0 application. In this section, we will focus on navigating through data using DomainDataSource.
The above creates a grid layout with three rows. I would like to reserve the first row for textboxes (and labels), the second for navigation buttons and the last for the data grid. Proceeding further, we have the following:
This is another grid layout which sits in the first row of the previous grid. This is mainly used to lay out labels and textboxes. All labels go into the first column and textboxes into the second, and we are planning for four pairs of labels and textboxes (each pair would occupy a row with a label and a textbox).
Partial Public Class DdsNavigation Inherits UserControl
Public Sub New() InitializeComponent() End Sub
Private Sub EmpDomDataSource_LoadedData(ByVal sender As Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles EmpDomDataSource.LoadedData If e.HasError Then System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK) e.MarkErrorAsHandled() End If End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPrevious.Click If Me.EmpDomDataSource.DataView.CurrentPosition > 0 Then Me.EmpDomDataSource.DataView.MoveCurrentToPrevious() End If End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNext.Click If Me.EmpDomDataSource.DataView.CurrentPosition < Me.EmpDomDataSource.DataView.Count Then Me.EmpDomDataSource.DataView.MoveCurrentToNext() End If End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnFirst.Click Me.EmpDomDataSource.DataView.MoveCurrentToFirst() End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLast.Click Me.EmpDomDataSource.DataView.MoveCurrentToLast() End Sub
End Class
The next section explains previous code.
Once we execute the above code, the output should look similar to the following:
Navigating through data using DomainDataSource: code-behind explanation
This section explains the code in the previous section.
“DomaindataSource” contains a special property called “CurrentItem.” This always holds the “Current” item, which acts as the main source of binding. Our textboxes actually get bound to this property. You can observe the same from the following code:
If we want our textboxes (say bound controls) to show other objects, we need to change the “CurrentItem” to the respective one. “DomainDatasource” has another property, “DataView,” which internally contains following methods:
MoveCurrentToFirst
MoveCurrentToNext
MoveCurrentToPrevious
MoveCurrentToLast
MoveCurrentToPosition
MoveCurrentTo
The above methods move context to another object in the list. The movement will be always relative to “CurrentItem.”
“MoveCurrentTo” is very helpful if we need to move our context directly to a known object (it accepts object as a parameter). “MoveCurrentToPostion” moves current context to the index we specify as an argument. Let us modify our form to demonstrate “MoveCurrentToPostion.”
Modify the stack panel so that it looks like the following:
The above adds a couple more controls to stack panel. Now, the design should look like the following:
Let us modify code-behind to add a new event as follows:
Private Sub btnGoTo_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnGoTo.Click If Me.txtGotoPos.Text > Me.EmpDomDataSource.DataView.Count - 1 OrElse Me.txtGotoPos.Text < 0 Then MessageBox.Show("Invalid position") Else Me.EmpDomDataSource.DataView.MoveCurrentToPosition(Me.txtGotoPos.Text) End If End Sub
Once we execute the form and provide 3 as input, the output should look like the following:
My upcoming articles will focus more on using the “DomainDataSource” element. I hope you enjoyed the article; any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com/.