[corlib] Avoid unnecessary ephemeron array resizes
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Converters / ObjectToModelValueConverter.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Converters
6 {
7     using System.Diagnostics;
8     using System.Globalization;
9     using System.Windows.Data;
10     using System.Activities.Presentation.Model;
11     using System.Activities.Presentation.Internal.PropertyEditing.Model;
12
13     //This value converter is used in propertygrid scenarios to convert an object into a ModelItem.
14     //It converts from ModelPropertyEntry to ModelItem that owns the property
15     //The first binding is a two way binding with the PropertyValue object 
16     //The second binding is a one way binding with the ModelPropertyEntry.
17     public class ObjectToModelValueConverter : IMultiValueConverter
18     {
19
20         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
21         {
22             ModelItem convertedValue = null;
23             if (values[1] != null)
24             {
25                 ModelPropertyEntry modelPropertyEntry = values[1] as ModelPropertyEntry;
26                 if (modelPropertyEntry != null)
27                 {
28                     ModelProperty property = modelPropertyEntry.FirstModelProperty;
29                     if (property != null)
30                     {
31                         convertedValue = property.Value;
32                     }
33                 }
34             }
35             return convertedValue;
36         }
37
38         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
39         {
40             object[] returnValue = new object[] { null };
41             if (value != null)
42             {
43                 returnValue[0] = ((ModelItem)value).GetCurrentValue();
44             }
45             return returnValue;
46         }
47     }
48 }