1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors
7 using System.Collections;
8 using System.ComponentModel;
9 using System.Diagnostics;
10 using System.Diagnostics.CodeAnalysis;
11 using System.Globalization;
12 using System.Windows.Data;
15 using System.Activities.Presentation;
16 using System.Activities.Presentation.PropertyEditing;
17 using System.Activities.Presentation.Internal.Properties;
18 using System.Activities.Presentation.Internal.PropertyEditing.Model;
21 // Retrieves StandardValues from a passed in PropertyValue, making sure that if a TypeConverter
22 // exists and if it supports ConvertToString() method, it will be called on each value contained
23 // in the StandardValues collection.
24 // This class is instantiated through XAML
26 [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
27 internal class PropertyValueToStandardValuesConverter : IValueConverter
29 // IValueConverter Members
31 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
34 Fx.Assert(typeof(IEnumerable).IsAssignableFrom(targetType), "Expecting IEnumerable as the targetType");
36 PropertyValue propertyValue = value as PropertyValue;
37 if (propertyValue == null)
42 ModelPropertyEntryBase parentProperty = (ModelPropertyEntryBase)propertyValue.ParentProperty;
43 return new ConvertedStandardValuesCollection(parentProperty, culture);
46 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
48 throw FxTrace.Exception.AsError(new InvalidOperationException());
53 // An implementation of ICollection that defers access to the standard
54 // values collection. Accessing the StandardValues collection may be
55 // expensive, so we defer it until we absolutely need it.
57 private class ConvertedStandardValuesCollection : ICollection
60 private ModelPropertyEntryBase _property;
61 private CultureInfo _culture;
62 private ICollection _contents;
64 internal ConvertedStandardValuesCollection(ModelPropertyEntryBase property, CultureInfo culture)
72 get { return Contents.Count; }
75 public bool IsSynchronized
80 public object SyncRoot
82 get { return Contents.SyncRoot; }
85 // IEnumerable Members
87 private ICollection Contents
90 if (_contents == null)
92 ICollection standardValues = _property.StandardValues;
93 TypeConverter converter = _property.Converter;
95 if (standardValues != null && (converter == null || !converter.CanConvertTo(typeof(string))))
97 _contents = standardValues;
101 ArrayList convertedStandardValues = new ArrayList(standardValues == null ? 0 : standardValues.Count);
102 if (standardValues != null)
104 foreach (object standardValue in standardValues)
106 convertedStandardValues.Add(converter.ConvertToString(null, _culture, standardValue));
109 _contents = convertedStandardValues;
112 // PS 107537: Special-case handling for nullable enum types
113 if (EditorUtilities.IsNullableEnumType(_property.PropertyType))
115 ArrayList filteredStandardValues = new ArrayList();
116 filteredStandardValues.Add(EditorUtilities.NullString);
117 foreach (var i in (ArrayList)_contents)
121 filteredStandardValues.Add(i);
124 _contents = filteredStandardValues;
132 // ICollection Members
134 public void CopyTo(Array array, int index)
136 Contents.CopyTo(array, index);
139 public IEnumerator GetEnumerator()
141 return Contents.GetEnumerator();