Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

MVVM and the WPF DataGrid

, 23 Sep 2009
Using MVVM, particularly with the WPF DataGrid
datagridopsdemo.zip
DataGridOpsDemo
DataGridOpsDemo
bin
Debug
DataGridOpsDemo.exe
DataGridOpsDemo.vshost.exe
DataGridOpsDemo.vshost.exe.manifest
WPFToolkit.dll
Domain
Images
delete_item.png
Properties
Settings.settings
UtilityClasses
View
delete_item.png
new_item.png
ViewModel
Commands
Services
ValueConverters
Library
WPFToolkit.dll
using System.Collections.ObjectModel;
using System.Windows.Input;
using DataGridOpsDemo.ViewModel.Services;
using DataGridOpsDemo.ViewModel.Commands;

namespace DataGridOpsDemo
{
    public class MainWindowViewModel : ViewModelBase
    {
        #region Fields

        // Property variables
        private ObservableCollection<GroceryItem> p_GroceryList;
        private int p_ItemCount;

        #endregion

        #region Constructor

        public MainWindowViewModel()
        {
            this.Initialize();
        }

        #endregion

        #region Command Properties

        /// <summary>
        /// Deletes the currently-selected item from the Grocery List.
        /// </summary>
        public ICommand DeleteItem { get; set; }

        #endregion

        #region Data Properties

        /// <summary>
        /// A grocery list.
        /// </summary>
        public ObservableCollection<GroceryItem> GroceryList
        {
            get { return p_GroceryList; }

            set
            {
                p_GroceryList = value;
                base.RaisePropertyChangedEvent("GroceryList");
            }
        }

        /// <summary>
        /// The currently-selected grocery item.
        /// </summary>
        public GroceryItem SelectedItem { get; set; }

        /// <summary>
        /// The number of items in the grocery list.
        /// </summary>

        public int ItemCount
        {
            get { return p_ItemCount; }

            set
            {
                p_ItemCount = value;
                base.RaisePropertyChangedEvent("ItemCount");
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Updates the ItemCount Property when the GroceryList collection changes.
        /// </summary>
        void OnGroceryListChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Update item count
            this.ItemCount = this.GroceryList.Count;

            // Resequence list
            SequencingService.SetCollectionSequence(this.GroceryList);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Initializes this application.
        /// </summary>
        private void Initialize()
        {
            // Initialize commands
            this.DeleteItem = new DeleteItemCommand(this);

            // Create grocery list
            p_GroceryList = new ObservableCollection<GroceryItem>();

            // Subscribe to CollectionChanged event
            p_GroceryList.CollectionChanged += OnGroceryListChanged;

            // Add items to the list
            p_GroceryList.Add(new GroceryItem("Macaroni"));
            p_GroceryList.Add(new GroceryItem("Shredded Wheat"));
            p_GroceryList.Add(new GroceryItem("Fish Filets"));
            p_GroceryList.Add(new GroceryItem("Hamburger Buns"));
            p_GroceryList.Add(new GroceryItem("Whipped Cream"));
            p_GroceryList.Add(new GroceryItem("Soft Drinks"));
            p_GroceryList.Add(new GroceryItem("Bread"));
            p_GroceryList.Add(new GroceryItem("Ice Cream"));
            p_GroceryList.Add(new GroceryItem("Chocolate Pudding"));
            p_GroceryList.Add(new GroceryItem("Sliced Turkey"));
            p_GroceryList.Add(new GroceryItem("Turkey Dressing"));
            p_GroceryList.Add(new GroceryItem("Cranberry Sauce"));
            p_GroceryList.Add(new GroceryItem("Swiss Cheese"));
            p_GroceryList.Add(new GroceryItem("Mushrooms"));
            p_GroceryList.Add(new GroceryItem("Butter"));
            p_GroceryList.Add(new GroceryItem("Eggs"));
            p_GroceryList.Add(new GroceryItem("Potatoes"));
            p_GroceryList.Add(new GroceryItem("Onion"));

            // Initialize list index
            this.GroceryList = SequencingService.SetCollectionSequence(this.GroceryList);

            // Update bindings
            base.RaisePropertyChangedEvent("GroceryList");
        }

        #endregion
    }
}

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)

About the Author

David Veeneman
Software Developer (Senior) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 23 Sep 2009
Article Copyright 2009 by David Veeneman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid