1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Tango.Graphics2D
{
public class Drawing2DStackPanel : Drawing2DHost
{
private double _currentPosition = 0;
private double _maxPosition = 0;
public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register("Orientation", typeof(Orientation), typeof(Drawing2DStackPanel), new PropertyMetadata(Orientation.Vertical));
public override Point GetElementLocation(FrameworkElement element)
{
if (Orientation == Orientation.Vertical)
{
_currentPosition += element.Margin.Top;
Point location = new Point(0, 0);
if (element.HorizontalAlignment == HorizontalAlignment.Left)
{
location = new Point(element.Margin.Left, _currentPosition);
}
else if (element.HorizontalAlignment == HorizontalAlignment.Right)
{
location = new Point(ActualWidth - element.Width - element.Margin.Right, _currentPosition);
}
else
{
location = new Point((ActualWidth / 2) - (element.Width / 2) + element.Margin.Left - element.Margin.Right, _currentPosition);
}
_currentPosition += (element.Height + element.Margin.Bottom);
_maxPosition = Math.Max(_maxPosition, element.Width);
return location;
}
else
{
_currentPosition += element.Margin.Left;
Point location = new Point(0, 0);
if (element.VerticalAlignment == VerticalAlignment.Top)
{
location = new Point(_currentPosition, element.Margin.Top);
}
else if (element.VerticalAlignment == VerticalAlignment.Bottom)
{
location = new Point(_currentPosition, ActualHeight - element.Height - element.Margin.Bottom);
}
else
{
location = new Point(_currentPosition, (ActualHeight / 2) - (element.Height / 2) + element.Margin.Top - element.Margin.Bottom);
}
_currentPosition += (element.Width + element.Margin.Right);
_maxPosition = Math.Max(_maxPosition, element.Height);
return location;
}
}
protected override Size OnMeasure()
{
if (Orientation == Orientation.Vertical)
{
return new Size(HorizontalAlignment != HorizontalAlignment.Stretch ? _maxPosition : double.NaN, _currentPosition);
}
else
{
return new Size(_currentPosition, _maxPosition);
}
}
public override Size GetElementSize(FrameworkElement element)
{
double width = element.Width;
double height = element.Height;
if (double.IsNaN(width) && Orientation == Orientation.Vertical)
{
width = ActualWidth;
}
if (double.IsNaN(height) && Orientation == Orientation.Horizontal)
{
height = ActualHeight;
}
width -= (element.Margin.Left + element.Margin.Right);
height -= (element.Margin.Top + element.Margin.Bottom);
return new Size(width, height);
}
}
}
|