Imports TutorialsSilverlight.Web
Imports C1.Data
Imports C1.Silverlight.Data.RiaServices

Partial Public Class DataSourcesInCode
    Inherits UserControl
    
    ' This client scope will be used to load the data.
    Private _scope As RiaClientScope = App.ClientCache.CreateScope()

    Public Sub New()
        InitializeComponent()

        ' Bind the category combo box to the category view
        ' and show the products of the first category in the data grid.

        Dim viewCategories As ClientView(Of Category) = _scope.GetItems(Of Category)("GetCategories")

        comboBox1.DisplayMemberPath = "CategoryName"
        comboBox1.ItemsSource = viewCategories
    End Sub

    Private Sub BindGrid(categoryID As Integer)
        ' Filter products by CategoryID on the server
        ' and bind them to the data grid.
        dataGrid1.ItemsSource =
                (From p In _scope.GetItems(Of Product)("GetProducts").AsFiltered(Function(p As Product) p.CategoryID.HasValue AndAlso p.CategoryID.Value = categoryID)
                 Select New ProductInfo With
                 {
                     .ProductID = p.ProductID,
                     .ProductName = p.ProductName,
                     .CategoryID = p.CategoryID,
                     .CategoryName = p.Category.CategoryName,
                     .UnitPrice = p.UnitPrice,
                     .QuantityPerUnit = p.QuantityPerUnit,
                     .UnitsInStock = p.UnitsInStock,
                     .UnitsOnOrder = p.UnitsOnOrder
                 })
    End Sub


    Private Sub comboBox1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles comboBox1.SelectionChanged
        ' Rebind the data grid, display the products of the selected category.
        If comboBox1.SelectedValue IsNot Nothing Then
            BindGrid(CType(comboBox1.SelectedValue, Category).CategoryID)
        End If
    End Sub

    Private Sub btnSaveChanges_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSaveChanges.Click
        App.ClientCache.SaveChanges()
    End Sub
End Class
