private void OnConvertersCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// The 'Converters' collection has been modified, so validate that each value converter it now
// contains is decorated with ValueConversionAttribute and then cache the attribute value.
IList convertersToProcess = null;
if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace)
{
convertersToProcess = e.NewItems;
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (IValueConverter converter in e.OldItems)
{
_cachedAttributes.Remove(converter);
}
}
else if (e.Action == NotifyCollectionChangedAction.Reset)
{
_cachedAttributes.Clear();
convertersToProcess = _converters;
}
if (convertersToProcess != null && convertersToProcess.Count > 0)
{
foreach (IValueConverter converter in convertersToProcess)
{
object[] attributes = converter.GetType().GetCustomAttributes(typeof (ValueConversionAttribute), false);
// Maybe it is 'beter' (more robust) to use a default ValueConversion(typeof(object), typeof(bool)) attribute.
if (attributes.Length != 1)
{
throw Log.ErrorAndCreateException<InvalidOperationException>("All value converters added to a ValueConverterGroup must be decorated with the ValueConversionAttribute attribute exactly once.");
}
_cachedAttributes.Add(converter, attributes[0] as ValueConversionAttribute);
}
}
}
#endregion