/Foggy.Desktop/Converters/IgnoreNewItemPlaceholderConverter.cs

http://foggy.codeplex.com · C# · 60 lines · 25 code · 5 blank · 30 comment · 4 complexity · fed2736291907ead72b969296aa9a5ee MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Foggy.Converters
  6. {
  7. using System.Windows;
  8. using System.Windows.Data;
  9. /// <summary>
  10. /// Workaround for a bug in the WPF DataGrid (WPF Toolkit version).
  11. /// </summary>
  12. /// <remarks>The WPF DataGrid (WPF Toolkit version) throws a FormatException if a view model
  13. /// includes a SelectedItem property. This value converter works aroun that bug. The value
  14. /// converter is taken from Nigel Spencer's Blog, which identifies the bug and provides this
  15. /// solution. See: http://blog.spencen.com/2009/04/30/problems-binding-to-selectedvalue-with-microsoftrsquos-wpf-datagrid.aspx?results=1(</remarks>
  16. public class IgnoreNewItemPlaceHolderConverter : IValueConverter
  17. {
  18. /// <summary>
  19. /// New item placeholder name
  20. /// </summary>
  21. private const string NewItemPlaceholderName = "{NewItemPlaceholder}";
  22. /// <summary>
  23. /// Converts a value.
  24. /// </summary>
  25. /// <param name="value">The value produced by the binding source.</param>
  26. /// <param name="targetType">The type of the binding target property.</param>
  27. /// <param name="parameter">The converter parameter to use.</param>
  28. /// <param name="culture">The culture to use in the converter.</param>
  29. /// <returns>
  30. /// A converted value. If the method returns null, the valid null value is used.
  31. /// </returns>
  32. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  33. {
  34. return value;
  35. }
  36. /// <summary>
  37. /// Converts a value.
  38. /// </summary>
  39. /// <param name="value">The value that is produced by the binding target.</param>
  40. /// <param name="targetType">The type to convert to.</param>
  41. /// <param name="parameter">The converter parameter to use.</param>
  42. /// <param name="culture">The culture to use in the converter.</param>
  43. /// <returns>
  44. /// A converted value. If the method returns null, the valid null value is used.
  45. /// </returns>
  46. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  47. {
  48. if (value != null && value.ToString() == NewItemPlaceholderName)
  49. {
  50. value = DependencyProperty.UnsetValue;
  51. }
  52. return value;
  53. }
  54. }
  55. }