Click here to Skip to main content
15,894,720 members
Articles / Desktop Programming / WPF

Row Header and Cell Retrieval in the WPF Toolkit DataGrid Using the MVVM Pattern

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
13 Apr 2011CPOL2 min read 36.5K   1.6K   14  
This article shows the implementation of a row header in a grid based on data grouping, and retrieving cell information from a cell-based grid.
using System.Windows;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Data;
using System.ComponentModel;
using RowHeaderGrid.Models;
using RowHeaderGrid.Commands;
using RowHeaderGrid.Views;
using RowHeaderGrid.Utils;
using RowHeaderGrid.DataAccess;
using System.Collections.ObjectModel;
using Microsoft.Windows.Controls;

namespace RowHeaderGrid.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private DelegateCommand exitCommand;
        public DelegateCommand<DataGridItem> CellClickCommand { get; private set; }

        IList<Item> _items;
        public IList<Item> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                base.OnPropertyChanged("Items");
            }
        }

        #region Constructor

        public MainViewModel()
        {
            _items = DataAccess.DataAccess.LoadItems();
            CellClickCommand = new DelegateCommand<DataGridItem>(CellClickSelection, CanCellClickSelection);
        }

        #endregion

        public ICommand ExitCommand
        {
            get
            {
                if (exitCommand == null)
                {
                    exitCommand = new DelegateCommand(Exit);
                }
                return exitCommand;
            }
        }

        private void Exit()
        {
            Application.Current.Shutdown();
        }

        private void CellClickSelection(DataGridItem item)
        {
            MessageBox.Show("Cell's column is " + item.Gridcell.Column.Header.ToString() +
                            " it's ID is " + ((RowHeaderGrid.Models.Item)(item.Gridrow.Item)).ID);
        }

        private bool CanCellClickSelection(DataGridItem cell)
        {
            return true;
        }
    }
}

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
Web Developer
United States United States
Yunyou Yao is a Senior IT Specialist in Houston, USA.

Comments and Discussions