Click here to Skip to main content
11,639,765 members (59,241 online)
Click here to Skip to main content
Add your own
alternative version

MVVM and the WPF DataGrid

, 23 Sep 2009 CPOL 253.7K 20.1K 189
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;
using System.Windows;
using System.Windows.Data;

namespace DataGridOpsDemo.ValueConverters
{
    /// <summary>
    /// Workaround for a bug in the WPF DataGrid (WPF Toolkit version).
    /// </summary>
    /// <remarks>The WPF DataGrid (WPF Toolkit version) throws a FormatException if a view model
    /// includes a SelectedItem property. This value converter works aroun that bug. The value
    /// converter is taken from Nigel Spencer's Blog, which identifies the bug and provides this
    /// solution. See: http://blog.spencen.com/2009/04/30/problems-binding-to-selectedvalue-with-microsoftrsquos-wpf-datagrid.aspx?results=1(</remarks>
    public class IgnoreNewItemPlaceHolderConverter : IValueConverter
    {
        private const string NewItemPlaceholderName = "{NewItemPlaceholder}";

        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return value;
        }

        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            if (value != null && value.ToString() == NewItemPlaceholderName)
            {
                value = DependencyProperty.UnsetValue;
            }
            return value;
        }
    }
}

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)

Share

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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 23 Sep 2009
Article Copyright 2009 by David Veeneman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid