[corlib] Avoid unnecessary ephemeron array resizes
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / FromExpression / Framework / ValueEditors / ValueEditorUtils.cs
1 // -------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 // -------------------------------------------------------------------
4
5 //Cider comment:
6 //  - Removed class MouseCursor
7
8 //From \\authoring\Sparkle\Source\1.0.1083.0\Common\Source\Framework\ValueEditors
9 namespace System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.ValueEditors
10 {
11     using System;
12     using System.ComponentModel;
13     using System.Runtime.InteropServices;
14     using System.Windows;
15     using System.Windows.Data;
16     using System.Windows.Input;
17     using System.Windows.Interop;
18     using System.Windows.Media;
19
20     internal enum UpdateBindingType
21     {
22         Source,
23         Target,
24         Both
25     }
26     internal static class ValueEditorUtils
27     {
28         // This property determines whether the commit keys (enter and escape) are marked handled by value editors or not.  It inherits
29         // so can be set at any point in the tree, and all ValueEditors below that point in the UI will use this behavior.
30         public static readonly DependencyProperty HandlesCommitKeysProperty = DependencyProperty.RegisterAttached("HandlesCommitKeys", typeof(bool), typeof(ValueEditorUtils), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
31
32         public static bool GetHandlesCommitKeys(DependencyObject dependencyObject)
33         {
34             return (bool)dependencyObject.GetValue(ValueEditorUtils.HandlesCommitKeysProperty);
35         }
36
37         public static void SetHandlesCommitKeys(DependencyObject dependencyObject, bool value)
38         {
39             dependencyObject.SetValue(ValueEditorUtils.HandlesCommitKeysProperty, value);
40         }
41
42         public static void UpdateBinding(FrameworkElement element, DependencyProperty property, bool updateSource)
43         {
44             ValueEditorUtils.UpdateBinding(element, property, (updateSource ? UpdateBindingType.Both : UpdateBindingType.Target));
45         }
46
47         public static void UpdateBinding(FrameworkElement element, DependencyProperty property, UpdateBindingType updateType)
48         {
49             BindingExpression bindingExpression = element.GetBindingExpression(property);
50             if (bindingExpression != null)
51             {
52                 // If desired, push the current text value to the source of the binding.
53                 if (updateType == UpdateBindingType.Source || updateType == UpdateBindingType.Both)
54                 {
55                     bindingExpression.UpdateSource();
56                 }
57
58                 // Update the text from the source of the binding.
59                 if (updateType == UpdateBindingType.Target || updateType == UpdateBindingType.Both)
60                 {
61                     bindingExpression.UpdateTarget();
62                 }
63             }
64         }
65
66         public static void ExecuteCommand(ICommand command, IInputElement element, object parameter)
67         {
68             RoutedCommand routedCommand = command as RoutedCommand;
69             if (routedCommand != null)
70             {
71                 if (routedCommand.CanExecute(parameter, element))
72                 {
73                     routedCommand.Execute(parameter, element);
74                 }
75             }
76             else
77             {
78                 if (command != null && command.CanExecute(parameter))
79                 {
80                     command.Execute(parameter);
81                 }
82             }
83         }
84     }
85
86
87 }