1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.ServiceModel.Activities.Presentation
6 using System.Collections.Generic;
8 using System.ServiceModel;
9 using System.ServiceModel.Channels;
11 using System.Windows.Controls;
12 using System.Activities.Presentation.Model;
13 using System.Configuration;
14 using System.ServiceModel.Configuration;
15 using System.Activities.Presentation;
17 partial class BindingEditor
19 public static readonly DependencyProperty BindingProperty =
20 DependencyProperty.Register("Binding",
22 typeof(BindingEditor),
23 new PropertyMetadata(OnBindingChanged));
25 List<BindingDescriptor> bindingElements = new List<BindingDescriptor>();
29 public BindingEditor()
31 InitializeComponent();
36 get { return GetValue(BindingProperty); }
37 set { SetValue(BindingProperty, value); }
44 this.bindingElements.Add(new BindingDescriptor { BindingName = (string)(this.TryFindResource("bindingEditorEmptyBindingLabel") ?? "none"), Value = null });
45 Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
46 ServiceModelSectionGroup section = ServiceModelSectionGroup.GetSectionGroup(machineConfig);
47 if (null != section && null != section.Bindings)
49 this.bindingElements.AddRange(section.Bindings.BindingCollections
50 .OrderBy(p => p.BindingName)
51 .Select<BindingCollectionElement, BindingDescriptor>(p => new BindingDescriptor() { BindingName = p.BindingName, Value = p }));
54 catch (ConfigurationErrorsException err)
56 ErrorReporting.ShowErrorMessage(err.Message);
60 protected override void OnInitialized(EventArgs e)
62 base.OnInitialized(e);
63 this.isInitializing = true;
65 this.ItemsSource = this.bindingElements;
66 this.SelectedIndex = 0;
67 this.isInitializing = false;
70 protected override void OnSelectionChanged(SelectionChangedEventArgs e)
72 base.OnSelectionChanged(e);
73 if (!this.isInitializing)
76 BindingDescriptor entry = (BindingDescriptor)e.AddedItems[0];
77 if (null == entry.Value)
81 // try to avoid blowing away any binding that has been custom-tweaked in XAML.
82 else if (Binding == null || !(Binding is ModelItem) || !((ModelItem)Binding).ItemType.Equals(entry.Value.BindingType))
84 Binding instance = (Binding)Activator.CreateInstance(entry.Value.BindingType);
85 instance.Name = entry.BindingName;
91 static void OnBindingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
93 BindingEditor editor = (BindingEditor)sender;
94 object newValue = args.NewValue;
96 Type bindingType = null;
97 ModelItem item = newValue as ModelItem;
98 string bindingName = null;
101 bindingType = item.ItemType;
102 bindingName = (string)item.Properties["Name"].ComputedValue;
104 else if (newValue != null)
106 bindingType = newValue.GetType();
107 if (typeof(Binding).IsAssignableFrom(bindingType))
109 bindingName = ((Binding)newValue).Name;
113 // Make combo appear empty if the binding is not one of the ones known to us, e.g., has been custom-tweaked in XAML.
114 BindingDescriptor toSelect = null;
115 Func<BindingDescriptor, bool> where = p => null != p.Value && p.Value.BindingType == bindingType;
116 if (editor.bindingElements.Count(where) > 1)
118 toSelect = editor.bindingElements.Where(where).Where(p => string.Equals(p.BindingName, bindingName)).FirstOrDefault();
122 toSelect = editor.bindingElements.Where(where).FirstOrDefault();
124 //prevent OnSelectionChanged now - the binding is set directly to the object, no need to set again through event handler
125 editor.isInitializing = true;
126 if (null != toSelect)
128 editor.SelectedItem = toSelect;
132 editor.SelectedIndex = 0;
134 //allow selection changed events to be consumed again
135 editor.isInitializing = false;
138 sealed class BindingDescriptor
140 public string BindingName
146 public BindingCollectionElement Value
152 public override string ToString()