comparison Chronosv2/source/Extensions/Windows/PanelExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children 09d18d6e5f40
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 /*
2 The MIT License
3
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 using System;
26 using System.Windows;
27 using System.Windows.Controls;
28
29 namespace Chronos.Extensions.Windows
30 {
31 /// <summary>
32 /// Extensions methods for the WPF <see cref="Panel"/> control.
33 /// </summary>
34 public static class PanelExtensions
35 {
36 #region · Extension Methods ·
37
38 /// <summary>
39 /// Brings the element to the front of the z-order.
40 /// </summary>
41 /// <param name="panel">The canvas.</param>
42 /// <param name="element">The element.</param>
43 public static void BringToFront(this Panel panel, UIElement element)
44 {
45 int index = Panel.GetZIndex(element);
46 Panel.SetZIndex(element, panel.Children.Count + 1);
47
48 #region · Define ZIndex Order ·
49
50 UIElement[] sortAux = new UIElement[panel.Children.Count];
51
52 panel.Children.CopyTo(sortAux, 0);
53
54 Array.Sort<UIElement>(sortAux, new Comparison<UIElement>(delegate(UIElement a, UIElement b)
55 {
56 int aIndex = Canvas.GetZIndex(a);
57 int bIndex = Canvas.GetZIndex(b);
58
59 if (aIndex > bIndex)
60 {
61 return 1;
62 }
63 else if (aIndex == bIndex)
64 {
65 return 0;
66 }
67 else
68 {
69 return -1;
70 }
71 }));
72
73 #endregion
74
75 for (int i = 0; i < sortAux.Length; i++)
76 {
77 Panel.SetZIndex(sortAux[i], i);
78 }
79 }
80
81 /// <summary>
82 /// Brings the element to the bottom of the z-order.
83 /// </summary>
84 /// <param name="panel">The panel.</param>
85 /// <param name="element">The element.</param>
86 public static void BringToBottom(this Panel panel, UIElement element)
87 {
88 int index = Panel.GetZIndex(element);
89 Panel.SetZIndex(element, -1);
90
91 #region · Define ZIndex Order ·
92
93 UIElement[] sortAux = new UIElement[panel.Children.Count];
94
95 panel.Children.CopyTo(sortAux, 0);
96
97 Array.Sort<UIElement>(sortAux, new Comparison<UIElement>(delegate(UIElement a, UIElement b)
98 {
99 int aIndex = Canvas.GetZIndex(a);
100 int bIndex = Canvas.GetZIndex(b);
101
102 if (aIndex > bIndex)
103 {
104 return 1;
105 }
106 else if (aIndex == bIndex)
107 {
108 return 0;
109 }
110 else
111 {
112 return -1;
113 }
114 }));
115
116 #endregion
117
118 for (int i = 0; i < sortAux.Length; i++)
119 {
120 Panel.SetZIndex(sortAux[i], i);
121 }
122 }
123
124 /// <summary>
125 /// Moves the specified element to the given position.
126 /// </summary>
127 /// <param name="element">The element.</param>
128 /// <param name="position">The position.</param>
129 public static void Move(this UIElement element, Point position)
130 {
131 element.Move(position.X, position.Y);
132 }
133
134 /// <summary>
135 /// Moves the specified element to the given position.
136 /// </summary>
137 /// <param name="element">The element.</param>
138 /// <param name="left">The left.</param>
139 /// <param name="top">The top.</param>
140 public static void Move(this UIElement element, double left, double top)
141 {
142 element.MoveLeft(left);
143 element.MoveTop(top);
144 }
145
146 /// <summary>
147 /// Moves the specified element to the given left position.
148 /// </summary>
149 /// <param name="element">The element.</param>
150 /// <param name="left">The left.</param>
151 public static void MoveLeft(this UIElement element, double left)
152 {
153 Canvas.SetLeft(element, left);
154 }
155
156 /// <summary>
157 /// Moves the specified element to the given top position.
158 /// </summary>
159 /// <param name="element">The element.</param>
160 /// <param name="top">The top.</param>
161 public static void MoveTop(this UIElement element, double top)
162 {
163 Canvas.SetTop(element, top);
164 }
165
166 /// <summary>
167 /// Sets the value of the ZIndex attached property for a given object.
168 /// </summary>
169 /// <param name="element">The element.</param>
170 /// <param name="zindex">The zindex.</param>
171 public static void SetZIndex(this UIElement element, int zindex)
172 {
173 Canvas.SetZIndex(element, zindex);
174 }
175
176 /// <summary>
177 /// Gets the canvas left position.
178 /// </summary>
179 /// <param name="element">The element.</param>
180 /// <returns></returns>
181 public static Point GetPosition(this UIElement element)
182 {
183 return new Point(Canvas.GetLeft(element), Canvas.GetTop(element));
184 }
185
186 #endregion
187 }
188 }