In-Memory Data

The purpose of this tutorial is to show you how to create in-memory data and use it in the context of your WPF application. The following common tasks will be examined:

  • Creating in-memory data.

  • Setting it as a data source/data context in WPF application.

This tutorial will use objects and data from the Northwind database, which can be downloaded from here.

Creating In-Memory Data

Here is a sample code showing how to create in-memory data:

public List<Categories> categories = new List<Categories>(); 
public void CreateCategories() 
{ 
    Categories c = new Categories(); 
    c.CategoryID = 1; 
    c.CategoryName = "Beverages"; 
    c.Description = "Soft drinks, coffees, teas, beers, and ales"; 
    categories.Add( c ); 
    c = new Categories(); 
    c.CategoryID = 2; 
    c.CategoryName = "Condiments"; 
    c.Description = "Sweet and savory sauces, relishes, spreads, and seasonings"; 
    categories.Add( c ); 
    c = new Categories(); 
    c.CategoryID = 3; 
    c.CategoryName = "Confections"; 
    c.Description = "Desserts, candies, and sweet breads"; 
    categories.Add( c ); 
} 
Public categories As New List(Of Categories)() 
Public Sub CreateCategories() 
    Dim c As New Categories() 
    c.CategoryID = 1 
    c.CategoryName = "Beverages" 
    c.Description = "Soft drinks, coffees, teas, beers, and ales" 
    categories.Add(c) 
    c = New Categories() 
    c.CategoryID = 2 
    c.CategoryName = "Condiments" 
    c.Description = "Sweet and savory sauces, relishes, spreads, and seasonings" 
    categories.Add(c) 
    c = New Categories() 
    c.CategoryID = 3 
    c.CategoryName = "Confections" 
    c.Description = "Desserts, candies, and sweet breads" 
    categories.Add(c) 
End Sub 

Setting In-Memory Data as DataSource In WPF Application

There are numerous ways to set in-memory data as data source:

  • Using XAML. Here is a sample XAML code:

<UserControl.Resources> 
        <example:categories x:Key="DataSource"/>        
</UserControl.Resources> 
<Grid x:Name="LayoutRoot" Background="White"> 
        <telerikNavigation:RadTreeView x:Name="radTreeView" 
           ItemsSource="{Binding Source={StaticResource DataSource}"/> 
</Grid> 
  • In code-behind:

radTreeView.ItemsSource = categories; 
// Or 
radTreeView.DataContext = categories; 
radTreeView.ItemsSource = categories 
' Or' 
radTreeView.DataContext = categories 

See Also

In this article
Not finding the help you need? Improve this article