Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / WPF

WPF UserControl == DataTemplate!!!

Rate me:
Please Sign up or sign in to vote.
4.88/5 (41 votes)
8 Aug 2008CPOL6 min read 276.3K   6.9K   103  
Demos how to use a WPF UserControl as a DataTemplate.
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlAsDataTemplateDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        ObservableCollection<DataItem> items = new ObservableCollection<DataItem>();

        public Window1()
        {
            InitializeComponent();
            PopulateDataItems();
            peopleListBox.ItemsSource = items;
        }

        void PopulateDataItems()
        {
            items.Add(new DataItem("Jammer", 32));
            items.Add(new DataItem("John", 44));
            items.Add(new DataItem("Jane", 25));
            items.Add(new DataItem("Robert", 30));
            items.Add(new DataItem("Jezzer", 50));
            items.Add(new DataItem("James", 40));
            items.Add(new DataItem("Rebecca", 25));
            items.Add(new DataItem("Mark", 35));
            items.Add(new DataItem("Leah", 20));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer JamSoft Solution Ltd
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions