Observable Collections
page 7 of 9
by frans eilering
Feedback
Average Rating: 
Views (Total / Last 10 Days): 40684/ 50

ObservableCollection

Insert a break point in the mainpage.aspx.vb code

And put e.result in your watch window by dragging or by right clicking the text and selecting "Add watch".

e.result is of type ObservableCollection. The way I interpret this type is that Silverlight keeps a link to this collection. When we change this collection, it will be visible in the datagrid without having to bind it again.

New code for Mainpage.aspx.vb

Imports System.Collections.ObjectModel
Partial Public Class MainPage
    Inherits UserControl
 
    Public WithEvents MyList As ObservableCollection(Of ServiceReference1.Contact)
    Public Sub New()
        InitializeComponent()
        Dim proxy As ServiceReference1.Service1Client = New 
ServiceReference1.Service1Client
        AddHandler proxy.FindContactListCompleted, AddressOf GetContactcompleted
        proxy.FindContactListAsync()
 
    End Sub
 
    Sub GetContactcompleted(ByVal sender As Object, ByVal e As 
ServiceReference1.FindContactListCompletedEventArgs)
        MyList = e.Result
        DataGrid1.ItemsSource = MyList
    End Sub
End Class

Here a public variable MyList of type ObservableCollection is inserted. It is part of the System.Collections.ObjectModel which is imported.

When running your application, the result will be the same. But changing the content of the datagrid will automatically change the content of MyList. And this can be handled in code.

New code for Mainpage.aspx.vb

Imports System.Collections.ObjectModel
Partial Public Class MainPage
    Inherits UserControl
 
    Public WithEvents MyList As ObservableCollection(Of ServiceReference1.Contact)
    Public Sub New()
        InitializeComponent()
        Dim proxy As ServiceReference1.Service1Client = New 
ServiceReference1.Service1Client
        
        AddHandler proxy.FindContactListCompleted, AddressOf GetContactcompleted
        proxy.FindContactListAsync()
    End Sub
 
    Sub GetContactcompleted(ByVal sender As Object, ByVal e As 
ServiceReference1.FindContactListCompletedEventArgs)
        MyList = e.Result
        DataGrid1.ItemsSource = MyList
 
        MyList.Add(New ServiceReference1.Contact() With { _
        .IDContact = 101, _
        .Name = "new", _
        .Address = "", _
        .City = "" _
       })
 
    End Sub
End Class
 

In this code a new record is inserted into MyList. Although Datagrid1 has been connected before to MyList, changes in MyList still will be visible in the datagrid.

I would like to hide the third column in the datagrid. It should not be visible. I would expect hiding the column in

   Sub GetContactcompleted(ByVal sender As Object, ByVal e As 
ServiceReference1.FindContactListCompletedEventArgs)
        MyList = e.Result
        DataGrid1.ItemsSource = MyList
 
        MyList.Add(New ServiceReference1.Contact() With { _
        .IDContact = 101, _
        .Name = "new", _
        .Address = "", _
        .City = "" _
       })
 
        DataGrid1.Columns(3).Visibility = Windows.Visibility.Collapsed
 
    End Sub

Would hide this column.

But this will result in an error.

Datagrid columns are created after the entire code is finished and can not be handled in this subroutine. A solution may be to manually create the columns in the datagrid. This will be shown later.

What you can do is change the result from the service using ILinq

Using ILinq in the result

  Sub GetContactcompleted(ByVal sender As Object, ByVal e As 
ServiceReference1.FindContactListCompletedEventArgs)
        MyList = e.Result
 
        MyList.Add(New ServiceReference1.Contact() With { _
        .IDContact = 101, _
        .Name = "new", _
        .Address = "", _
        .City = "" _
       })
 
        Dim TopData = From NewData In MyList
              Order By NewData.Name Descending
              Take (2)
        DataGrid1.ItemsSource = TopData
 
    End Sub

Gives this


View Entire Article

User Comments

Title: C# Code example   
Name: eilering
Date: 2011-09-16 3:50:25 AM
Comment:
The VB code is very basic. If you copy the code and use a convertor like
http://converter.telerik.com/
then you can paste it and you get the C# code.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 7:57:57 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search