ObjectPropertiesToCollectionViewConverter.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Markup;
  6. using System.Windows.Data;
  7. using System.Reflection;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using MatrixIO.IO.Bmff;
  11. namespace BmffViewer
  12. {
  13. public class ObjectPropertiesToCollectionViewConverter : MarkupExtension, IValueConverter
  14. {
  15. public ObjectPropertiesToCollectionViewConverter() : base() { }
  16. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  17. {
  18. List<ObjectProperty> objectProperties = new List<ObjectProperty>();
  19. if (value != null)
  20. {
  21. foreach (PropertyInfo property in value.GetType().GetProperties())
  22. {
  23. if (value is ISuperBox && property.Name == "Children") continue;
  24. if (value is ITableBox && property.Name == "Entries") continue;
  25. if ((value is Box || value is IMediaFile) && property.Name == "SourceStream") continue;
  26. object propertyValue = property.GetValue(value, null);
  27. IEnumerable propertyValues = propertyValue as IEnumerable;
  28. if (propertyValues != null && !(propertyValues is String))
  29. {
  30. List<string> stringValues = new List<string>();
  31. foreach (object item in propertyValues) stringValues.Add(item.ToString());
  32. objectProperties.Add(new ObjectProperty() { Name = property.Name, Value = String.Join(", ", stringValues), Class = property.DeclaringType.Name });
  33. }
  34. else objectProperties.Add(new ObjectProperty() { Name = property.Name, Value = propertyValue != null ? propertyValue.ToString() : null, Class = property.DeclaringType.Name });
  35. }
  36. }
  37. ICollectionView viewSource = CollectionViewSource.GetDefaultView(objectProperties);
  38. viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Class"));
  39. return viewSource;
  40. }
  41. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  42. {
  43. return null;
  44. }
  45. public override object ProvideValue(IServiceProvider serviceProvider)
  46. {
  47. return this;
  48. }
  49. public class ObjectProperty
  50. {
  51. public string Name { get; set; }
  52. public string Value { get; set; }
  53. public string Class { get; set; }
  54. }
  55. }
  56. }