PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Blocks/Configuration/Src/Design/ComponentModel/Editors/ElementCollectionEditor.xaml.cs

#
C# | 169 lines | 123 code | 24 blank | 22 comment | 8 complexity | c9d882cd14d8a5a720c78ec7841cecd5 MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices Enterprise Library
  3. // Core
  4. //===============================================================================
  5. // Copyright Š Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Windows;
  16. using System.Windows.Automation;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. using System.Windows.Shapes;
  25. using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.ViewModel;
  26. using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Controls;
  27. using System.ComponentModel;
  28. using System.Configuration;
  29. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
  30. namespace Microsoft.Practices.EnterpriseLibrary.Configuration.Design.ComponentModel.Editors
  31. {
  32. /// <summary>
  33. /// An editor that displays a collection of elements in a grid-like layout for editing as part of
  34. /// a parent element.
  35. /// <br/>
  36. /// This is used by the design-time infrastructure and is not intended to be used directly from your code.
  37. /// </summary>
  38. public partial class ElementCollectionEditor : UserControl
  39. {
  40. ElementCollectionViewModel viewModel;
  41. ///<summary>
  42. /// Initializes a new instance of <see cref="ElementCollectionEditor"/>.
  43. ///</summary>
  44. public ElementCollectionEditor()
  45. {
  46. InitializeComponent();
  47. }
  48. /// <summary>
  49. /// Establishing the grid based on the property count of the children.
  50. /// </summary>
  51. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "childProperty")]
  52. public override void OnApplyTemplate()
  53. {
  54. BindableProperty property = DataContext as BindableProperty;
  55. if (property == null) return;
  56. CustomEditorBinder.BindProperty(this, property);
  57. ElementProperty elementProperty = property.Property as ElementProperty;
  58. if (elementProperty != null)
  59. {
  60. viewModel = elementProperty.DeclaringElement.ChildElement(elementProperty.DeclaringProperty.Name) as ElementCollectionViewModel;
  61. }
  62. if (viewModel != null)
  63. {
  64. var properties = TypeDescriptor.GetProperties(viewModel.CollectionElementType).OfType<PropertyDescriptor>().Where(x => x.Attributes.OfType<ConfigurationPropertyAttribute>().Any()).ToArray();
  65. foreach (var childProperty in properties.Where(x => x.IsBrowsable))
  66. {
  67. Collection.ColumnDefinitions.Add(new ColumnDefinition()
  68. {
  69. Width = new GridLength(75, GridUnitType.Star)
  70. });
  71. }
  72. Collection.ColumnDefinitions.Add(new ColumnDefinition()
  73. {
  74. Width = new GridLength(25)
  75. });
  76. Redraw();
  77. viewModel.ChildElementsCollectionChange += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ChildElements_CollectionChanged);
  78. }
  79. }
  80. void ChildElements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  81. {
  82. Collection.Children.Clear();
  83. Collection.RowDefinitions.Clear();
  84. Redraw();
  85. }
  86. private void Redraw()
  87. {
  88. var properties = TypeDescriptor.GetProperties(viewModel.CollectionElementType).OfType<PropertyDescriptor>().Where(x => x.Attributes.OfType<ConfigurationPropertyAttribute>().Any()).ToArray();
  89. for (int n = 0; n <= viewModel.ChildElements.Count() + 1; n++)
  90. {
  91. Collection.RowDefinitions.Add(new RowDefinition());
  92. }
  93. int i = 0;
  94. foreach (var property in properties)
  95. {
  96. var label = new Label() { Content = property.DisplayName };
  97. Collection.Children.Add(label);
  98. label.SetValue(Grid.RowProperty, 0);
  99. label.SetValue(Grid.ColumnProperty, i);
  100. var gridSplitter = new GridSplitter() { Width = 2, HorizontalAlignment = HorizontalAlignment.Right };
  101. Collection.Children.Add(gridSplitter);
  102. gridSplitter.Focusable = false;
  103. gridSplitter.SetValue(Grid.RowProperty, 0);
  104. gridSplitter.SetValue(Grid.ColumnProperty, i);
  105. gridSplitter.SetValue(Grid.RowSpanProperty, viewModel.ChildElements.Count() + 1);
  106. i++;
  107. }
  108. ContextMenuButton addButton = new ContextMenuButton();
  109. Collection.Children.Add(addButton);
  110. CommandModel addCommand = viewModel.AddCommands.First();
  111. addButton.Command = addCommand;
  112. addButton.SetValue(Grid.RowProperty, 0);
  113. addButton.SetValue(Grid.ColumnProperty, i);
  114. addButton.Style = FindResource("ContextAdderButtonMenuStyle") as Style;
  115. addButton.VerticalAlignment = VerticalAlignment.Center;
  116. addButton.SetValue(AutomationProperties.AutomationIdProperty, addCommand.Title);
  117. int j = 1;
  118. foreach (var element in viewModel.ChildElements)
  119. {
  120. i = 0;
  121. foreach (var propertyDescriptor in properties)
  122. {
  123. var property = element.Property(propertyDescriptor.Name);
  124. ContentControl contentControl = new ContentControl();
  125. contentControl.Focusable = false;
  126. Collection.Children.Add(contentControl);
  127. contentControl.SetValue(ContentControl.ContentProperty, property.BindableProperty);
  128. contentControl.SetValue(Grid.RowProperty, j);
  129. contentControl.SetValue(Grid.ColumnProperty, i);
  130. i++;
  131. }
  132. Button deleteButton = new Button();
  133. Collection.Children.Add(deleteButton);
  134. CommandModel deleteCommand = element.Commands.Where(x => x.Placement == CommandPlacement.ContextDelete).First();
  135. deleteButton.Command = deleteCommand;
  136. deleteButton.SetValue(Grid.RowProperty, j);
  137. deleteButton.SetValue(Grid.ColumnProperty, i);
  138. deleteButton.Style = FindResource("DeleteButtonStyle") as Style;
  139. deleteButton.VerticalAlignment = VerticalAlignment.Center;
  140. deleteButton.SetValue(AutomationProperties.AutomationIdProperty, deleteCommand.Title);
  141. j++;
  142. }
  143. }
  144. }
  145. }