New to Telerik UI for WPFStart a free 30-day trial

In-Memory Data

Updated on Sep 15, 2025

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.

Creating In-Memory Data

The following example shows how to setup a basic data model along with a view model.

Creating in-memory data

C#
	public class Category
    {
        public string CategoryName { get; internal set; }
        public int CategoryID { get; internal set; }
        public string Description { get; internal set; }
    }

	public class ViewModel
    {
        private ObservableCollection<Category> categories;

        public ObservableCollection<Category> Categories
        {
            get
            {
                if(this.categories == null)
                {
                    this.categories = this.CreateCategories();
                }
                return this.categories;
            }
        }

        private ObservableCollection<Category> CreateCategories()
        {
            var categories = new ObservableCollection<Category>();
            var c = new Category();
            c.CategoryID = 1;
            c.CategoryName = "Beverages";
            c.Description = "Soft drinks, coffees, teas, beers, and ales";
            categories.Add(c);
            c = new Category();
            c.CategoryID = 2;
            c.CategoryName = "Condiments";
            c.Description = "Sweet and savory sauces, relishes, spreads, and seasonings";
            categories.Add(c);
            c = new Category();
            c.CategoryID = 3;
            c.CategoryName = "Confections";
            c.Description = "Desserts, candies, and sweet breads";
            categories.Add(c);

            return categories;
        }
    }

Setting In-Memory Data as DataSource In WPF Application

You can set the in-memory data as a data source in xaml or in code.

Setting the data source in xaml

XAML
    <Grid>
	  	<Grid.DataContext>
            <local:ViewModel />
        </Grid.DataContext>
        <telerik:RadTreeView x:Name="radTreeView" 
                             ItemsSource="{Binding Categories}" 
                             DisplayMemberPath="CategoryName"/>
    </Grid>

Setting the data source in code

C#
	public MainWindow()
	{
		InitializeComponent();
		this.radTreeView.ItemsSource = new ViewModel().Categories;
	}

See Also