-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoClip.cs
68 lines (61 loc) · 2 KB
/
AutoClip.cs
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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
namespace StringTemplate
{
public class AutoClip
{
private List<UIElement> children;
private RectangleGeometry geometry;
private IClippable parent;
public AutoClip(IClippable parent)
{
this.parent = parent;
children = new List<UIElement>();
geometry = new RectangleGeometry();
geometry.Rect = new Rect(0, 0, parent.Width, parent.Height);
parent.Clip = geometry;
parent.OnPropertyChanged += new EventHandler<DependencyArgs>(OnPropertyChanged);
}
private void OnPropertyChanged(object sender, DependencyArgs e)
{
if (e.Property == Canvas.WidthProperty)
{
geometry = new RectangleGeometry();
geometry.Rect = new Rect(0, 0, Convert.ToDouble(e.NewValue), parent.Height);
parent.Clip = geometry;
}
else if (e.Property == Canvas.HeightProperty)
{
geometry = new RectangleGeometry();
geometry.Rect = new Rect(0, 0, parent.Width, Convert.ToDouble(e.NewValue));
parent.Clip = geometry;
}
else
{
return;
}
foreach (UIElement element in children)
{
Clip(element);
}
}
public void Clip(UIElement element)
{
if (!children.Contains(element))
{
children.Add(element);
}
RectangleGeometry clone = new RectangleGeometry();
clone.Rect = new Rect(geometry.Rect.Location, geometry.Rect.Size);
element.Clip = clone;
}
}
}