Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SynchronizeLargestSizeBehavior.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Windows;
6 
7 using SiliconStudio.Core.Extensions;
8 using SiliconStudio.Presentation.Core;
9 
10 namespace SiliconStudio.Presentation.Behaviors
11 {
12  /// <summary>
13  /// A behavior that allows to synchronize the width and/or height of FrameworkElements so that each instance has the width/height of the largest one.
14  /// </summary>
15  /// <remarks>Be careful while using this behavior, since the size of the FraneworkElements will always grow and never shrink.</remarks>
16  [Obsolete("This behavior is not maintained and should not be used.")]
17  public class SynchronizeLargestSizeBehavior : DeferredBehaviorBase<FrameworkElement>
18  {
19  private static readonly Dictionary<string, List<FrameworkElement>> ElementGroups = new Dictionary<string, List<FrameworkElement>>();
20  private readonly DependencyPropertyWatcher propertyWatcher = new DependencyPropertyWatcher();
21  private string groupName;
22 
23  /// <summary>
24  /// Gets or sets a group name for this instance. Every FrameworkElement of the same group will be synchronized.
25  /// </summary>
26  public string GroupName { get { return groupName; } set { UnregisterElement(); groupName = value; RegisterElement(); } }
27 
28  /// <summary>
29  /// Gets or sets whether the width of the FrameworkElements should be synchronized.
30  /// </summary>
31  public bool SynchronizeWidth { get; set; }
32 
33  /// <summary>
34  /// Gets or sets whether the height of the FrameworkElements should be synchronized.
35  /// </summary>
36  public bool SynchronizeHeight { get; set; }
37 
38  protected override void OnAttachedOverride()
39  {
40  propertyWatcher.Attach(AssociatedObject);
41  if (SynchronizeWidth)
42  {
43  propertyWatcher.RegisterValueChangedHandler(FrameworkElement.ActualWidthProperty, WidthChanged);
44  }
45  if (SynchronizeHeight)
46  {
47  propertyWatcher.RegisterValueChangedHandler(FrameworkElement.ActualHeightProperty, HeightChanged);
48  }
49  RegisterElement();
50  }
51 
52  protected override void OnDetachingOverride()
53  {
54  propertyWatcher.Detach();
55  UnregisterElement();
56  }
57 
58  private void RegisterElement()
59  {
60  if (AssociatedObject != null)
61  {
62  List<FrameworkElement> list = ElementGroups.GetOrCreateValue(GroupName);
63  list.Add(AssociatedObject);
64  if (SynchronizeWidth)
65  {
66  WidthChanged(AssociatedObject, EventArgs.Empty);
67  }
68  if (SynchronizeHeight)
69  {
70  HeightChanged(AssociatedObject, EventArgs.Empty);
71  }
72  }
73  }
74 
75  private void UnregisterElement()
76  {
77  if (AssociatedObject != null)
78  {
79  List<FrameworkElement> list;
80  if (ElementGroups.TryGetValue(GroupName, out list))
81  {
82  list.Remove(AssociatedObject);
83  }
84  }
85  }
86 
87  private void WidthChanged(object sender, EventArgs e)
88  {
89  List<FrameworkElement> list;
90  if (ElementGroups.TryGetValue(GroupName, out list))
91  {
92  // Prevent every element to resize every other element
93  ElementGroups.Remove(GroupName);
94  foreach (var element in list)
95  {
96  if (element.ActualWidth < AssociatedObject.ActualWidth)
97  element.Width = AssociatedObject.ActualWidth;
98  }
99  ElementGroups.Add(GroupName, list);
100  }
101  }
102 
103  private void HeightChanged(object sender, EventArgs e)
104  {
105  List<FrameworkElement> list;
106  if (ElementGroups.TryGetValue(GroupName, out list))
107  {
108  // Prevent every element to resize every other element
109  ElementGroups.Remove(GroupName);
110  foreach (var element in list)
111  {
112  if (element.ActualHeight < AssociatedObject.ActualHeight)
113  element.Height = AssociatedObject.ActualHeight;
114  }
115  ElementGroups.Add(GroupName, list);
116  }
117  }
118  }
119 }
A behavior that allows to synchronize the width and/or height of FrameworkElements so that each insta...