1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.Converters
8 using System.Collections.Generic;
9 using System.Globalization;
12 using System.Windows.Data;
13 using System.Activities.Presentation;
14 using System.Activities.Presentation.Model;
15 using System.Activities.Presentation.PropertyEditing;
17 using System.Diagnostics.CodeAnalysis;
19 public sealed class ModelPropertyEntryToModelItemConverter : IMultiValueConverter, IValueConverter
21 ModelPropertyEntryToOwnerActivityConverter propertyEntryConverter = new ModelPropertyEntryToOwnerActivityConverter();
23 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
25 PropertyEntry propertyEntry = value as PropertyEntry;
26 if (null == propertyEntry)
28 PropertyValue propertyValue = value as PropertyValue;
29 if (null != propertyValue)
31 propertyEntry = propertyValue.ParentProperty;
35 Container result = null;
36 if (null != propertyEntry)
38 ModelItem item = null;
39 ModelItem propertyParentItem = null;
40 EditingContext context = null;
41 GetPropertyData(propertyEntry, out item, out propertyParentItem, out context);
42 result = new Container(item, context, item.View, propertyEntry.PropertyValue);
47 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
49 CategoryEntry category = (CategoryEntry)
50 values.FirstOrDefault<object>(p => p != null && typeof(CategoryEntry).IsAssignableFrom(p.GetType()));
52 FrameworkElement categoryEditorVisual = (FrameworkElement)
53 values.FirstOrDefault<object>(p => p != null && typeof(FrameworkElement).IsAssignableFrom(p.GetType()));
55 Container result = null;
59 PropertyEntry property = GetPropertyEntry( category, parameter );
63 ModelItem item = null;
64 ModelItem propertyParentItem = null;
65 EditingContext context = null;
66 GetPropertyData(property, out item, out propertyParentItem, out context);
67 result = new Container(item, context, item.View, property.PropertyValue);
69 category.PropertyChanged += (sender, e) =>
71 PropertyEntry selectedProperty = this.GetPropertyEntry((CategoryEntry)sender, parameter);
72 if (null != selectedProperty)
74 this.UpdateCategoryEditorDataContext(selectedProperty, categoryEditorVisual, result);
82 PropertyEntry GetPropertyEntry(CategoryEntry category, object parameter)
84 PropertyEntry property = null;
85 IEnumerable<PropertyEntry> properties = (category == null ? null : category.Properties);
86 if (null != properties)
88 if (null == parameter)
90 property = properties.ElementAtOrDefault<PropertyEntry>(0);
94 property = properties.FirstOrDefault<PropertyEntry>(p => string.Equals(p.DisplayName, parameter));
100 void GetPropertyData(PropertyEntry property, out ModelItem activityItem, out ModelItem propertyParentItem, out EditingContext context)
102 activityItem = (ModelItem)this.propertyEntryConverter.Convert(property, typeof(ModelItem), false, null);
103 propertyParentItem = (ModelItem)this.propertyEntryConverter.Convert(property, typeof(ModelItem), true, null);
104 context = activityItem.GetEditingContext();
107 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
108 Justification = "Catch all exceptions to prevent crash.")]
109 [SuppressMessage("Reliability", "Reliability108:IsFatalRule",
110 Justification = "Catch all exceptions to prevent crash.")]
111 void UpdateCategoryEditorDataContext(PropertyEntry property, FrameworkElement editor, Container context)
115 editor.DataContext = null;
116 ModelItem modelItem = null;
117 ModelItem propertyParentItem = null;
118 EditingContext editingContext = null;
119 this.GetPropertyData(property, out modelItem, out propertyParentItem, out editingContext);
120 context.Context = editingContext;
121 context.WorkflowViewElement = (null == modelItem ? null : modelItem.View);
122 context.ModelItem = modelItem;
123 editor.DataContext = context;
125 catch (Exception err)
127 Fx.Assert(false, err.Message);
131 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
133 throw FxTrace.Exception.AsError(new NotSupportedException());
136 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
138 throw FxTrace.Exception.AsError(new NotSupportedException());
143 internal sealed class Container : DependencyObject
145 public static readonly DependencyProperty ModelItemProperty =
146 DependencyProperty.Register("ModelItem", typeof(ModelItem), typeof(Container), new UIPropertyMetadata(null));
148 public static readonly DependencyProperty ContextProperty =
149 DependencyProperty.Register("Context", typeof(EditingContext), typeof(Container), new UIPropertyMetadata(null));
151 public static readonly DependencyProperty WorkflowViewElementProperty =
152 DependencyProperty.Register("WorkflowViewElement", typeof(DependencyObject), typeof(Container), new UIPropertyMetadata(null));
154 public static readonly DependencyProperty PropertyValueProperty =
155 DependencyProperty.Register("PropertyValue", typeof(PropertyValue), typeof(Container), new UIPropertyMetadata(null));
157 public Container(ModelItem item, EditingContext context, DependencyObject viewElement, PropertyValue value)
159 this.ModelItem = item;
160 this.Context = context;
161 this.WorkflowViewElement = viewElement;
162 this.PropertyValue = value;
165 public ModelItem ModelItem
167 get { return (ModelItem)GetValue(ModelItemProperty); }
168 set { SetValue(ModelItemProperty, value); }
171 public EditingContext Context
173 get { return (EditingContext)GetValue(ContextProperty); }
174 set { SetValue(ContextProperty, value); }
177 public DependencyObject WorkflowViewElement
179 get { return (DependencyObject)GetValue(WorkflowViewElementProperty); }
180 set { SetValue(WorkflowViewElementProperty, value); }
183 public PropertyValue PropertyValue
185 get { return (PropertyValue)GetValue(PropertyValueProperty); }
186 set { SetValue(PropertyValueProperty, value); }