[corlib] Avoid unnecessary ephemeron array resizes
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Editors / quickitemtemplateselector.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors 
5 {
6     using System;
7     using System.Diagnostics.CodeAnalysis;
8     using System.Windows;
9     using System.Windows.Controls;
10     using System.Windows.Media;
11
12     using System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.ValueEditors;
13
14     // <summary>
15     // DataTemplateSelector we use in some instances of ChoiceEditor (namely sub-property editor)
16     // to detect whether the given item is being displayed in the popup or as an inline item.
17     // Based on that determination, it returns the appropriate DataTemplate.  This is a work-around
18     // for the problem where we can't determine which NewItemTypeFactory instantiated a given instance.
19     // Hence, we show the instance Type inline the ComboBox and the factory DisplayName in the drop-down.
20     // Ideally, we would want to use a different control to handle this scenario.
21     // </summary>
22     internal class QuickItemTemplateSelector : DataTemplateSelector 
23     {
24
25         private DataTemplate _popupTemplate;
26         private DataTemplate _inlineTemplate;
27
28         public DataTemplate PopupTemplate 
29         {
30             [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
31             get { return _popupTemplate; }
32             [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33             set { _popupTemplate = value; }
34         }
35
36         public DataTemplate InlineTemplate 
37         {
38             [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
39             get { return _inlineTemplate; }
40             [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
41             set { _inlineTemplate = value; }
42         }
43
44         public override DataTemplate SelectTemplate(object item, DependencyObject container) 
45         {
46             return HasChoiceEditorParent(container) ? _inlineTemplate : _popupTemplate;
47         }
48
49         private bool HasChoiceEditorParent(DependencyObject element) 
50         {
51             while (element != null) 
52             {
53                 element = VisualTreeHelper.GetParent(element);
54                 if (element != null && typeof(ChoiceEditor).IsAssignableFrom(element.GetType()))
55                 {
56                     return true;
57                 }
58             }
59
60             return false;
61         }
62     }
63 }