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

DraggableListView - Adding drag-to-scroll functionality to ListView in WPF

Rate me:
Please Sign up or sign in to vote.
4.88/5 (13 votes)
3 Oct 2008CPOL4 min read 104.1K   4.8K   43  
A custom ListView that scrolls when its contents are dragged on screen.
using System;
using System.Collections.Generic;
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 DraggableListViewSample
{
    /// <summary>
    /// Interaction logic for EmployeeListView.xaml
    /// </summary>
    public partial class EmployeeListView : System.Windows.Controls.Page
    {
        public EmployeeListView()
        {
            InitializeComponent();
        }
    }

    #region Data Source

    public class Employees : ObservableCollection<Employee>
    {
        public Employees()
        {
            for (int i = 0; i < 30; i++)
            {
                Add(new Employee(i * 6 + 1, "Charlie B.", "Developer", "(425)-555-8080", String.Format("{0}", i * 6 + 1) + "@contoso.com", true, "Boston", "USA"));
                Add(new Employee(i * 6 + 2, "Betty Y.", "Developer", "(425)-555-8081", String.Format("{0}", i * 6 + 2) + "@contoso.com", true, "New York", "USA"));
                Add(new Employee(i * 6 + 3, "Luke S.", "Developer", "(425)-555-8082", String.Format("{0}", i * 6 + 3) + "@contoso.com", false, "Rome", "Italy"));
                Add(new Employee(i * 6 + 4, "Kevin U.", "Lead Developer", "(425)-555-8083", String.Format("{0}", i * 6 + 4) + "@contoso.com", false, "Riverside", "USA"));
                Add(new Employee(i * 6 + 5, "Homer S.", "Tester", "(425)-555-8084", String.Format("{0}", i * 6 + 5) + "@contoso.com", true, "Paris", "France"));
                Add(new Employee(i * 6 + 6, "George C.", "Program Manager", "(425)-555-8085", String.Format("{0}", i * 6 + 6) + "@contoso.com", false, "Oslo", "Norway"));
            }
        }
    }

    public class Employee
    {
        public Employee()
        {
            _id = 0;
            _name = "Unknown";
            _position = "N/A";
            _telephone = "(425)-555-0000";
            _email = "unknown@contoso.com";
            _enabled = true;
            _city = "N/A";
            _country = "N/A";
        }

        public Employee(int id, string name, string position, string telephone, string email, bool enabled, string city, string country)
        {
            _id = id;
            _name = name;
            _position = position;
            _telephone = telephone;
            _email = email;
            _enabled = enabled;
            _city = city;
            _country = country;
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Position
        {
            get { return _position; }
            set { _position = value; }
        }

        private string _telephone;

        public string Telephone
        {
            get { return _telephone; }
            set { _telephone = value; }
        }

        private string _email;

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        private bool _enabled;

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        public string City
        {
            get { return _city; }
            set { _city = value; }
        }
        public string Country
        {
            get { return _country; }
            set { _country = value; }
        }

        public override string ToString()
        {
            return "Employee ID: " + _id.ToString() + "  Name: " + _name;
        }

        private int _id;
        private string _name;
        private string _position;
        private string _city;
        private string _country;
    }

    #endregion Data Source
}

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
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions