posts - 234, comments - 480, trackbacks - 56

My Links

News




I am born in Bangladesh and currently live in Melbourne, Australia. I am a co-founder and core developer of Pageflakes www.pageflakes.com and CEO at Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh.

I also created SmartCodeGenerator

Some of my articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Databinding using ObservableCollection<T>

Someone asked me today:
I have a text field and a drop down menu, based on the values of these 2 when i click on a button an api call is made to get the results. Now i want to display these results in a ListView using GridView. How can i use ObservableCollection to read the data when the search button is hit.

My Short Reply:

Creating an ObservableCollection is pretty straight forward.

1. For example say we have class Customer { id, name, address }

2. Now lets create a DataSrc that returns an ObservableCollection of Customer
public class CustomerDataSrc
{
 private ObservableCollection<Customer> _results = new
ObservableCollection<Customer>();
 public ObservableCollection<Customer> Customers {get { return _results; }}
 private void LoadCustomers()
 {
   IList<Customer> customers = YourDAL.FindAll();
   foreach (Customer customer in customers)
   {
     _results.Add(customer);
   }
 }
}

3. Declare an ObjectDataProvider in your XAML Page.Resources
<Page.Resources>
 <ObjectDataProvider x:Key="CustomerDataSrc"
                   d:IsDataSource="True"
                   ObjectType="{x:Type Client_DataSources:CustomerDataSrc}"/>
</Page.Resources>

4. Bind the ListView like this:
<ListView x:Name="dataGrid"
              ItemsSource="{Binding Path=Customers,
              Mode=Default,
              Source={StaticResource CustomerDataSrc}}">

5. In the code behind do this: (change according to the on button click event)

private void Page_Loaded(object sender, RoutedEventArgs e)
{
  ObjectDataProvider odp = this.FindResource("CustomerDataSrc") as ObjectDataProvider;
  _customerSrc = odp.ObjectInstance as CustomerDataSrc;
  _customerSrc.IsDesignTime = System.ComponentModel.DesignerProperties.GetIsInDesignMode(this);
  _customerSrc.LoadCustomers();
}




Hope this helps

Print | posted on Wednesday, October 10, 2007 1:08 PM |

Feedback

Gravatar

# re: Databinding using ObservableCollection<T>

Great..It really helps..
Thanks..
2/7/2008 1:48 AM | Asem
Gravatar

# re: Databinding using ObservableCollection<T>

What a Fuckall explaination...
8/2/2009 3:48 PM | Ghanta Singh
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: