forked from zedjia/DiagramDesigner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignerItem.cs
More file actions
183 lines (156 loc) · 6.66 KB
/
DesignerItem.cs
File metadata and controls
183 lines (156 loc) · 6.66 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using DiagramDesigner.Controls;
namespace DiagramDesigner
{
//These attributes identify the types of the named parts that are used for templating
[TemplatePart(Name = "PART_DragThumb", Type = typeof(DragThumb))]
[TemplatePart(Name = "PART_ResizeDecorator", Type = typeof(Control))]
[TemplatePart(Name = "PART_ConnectorDecorator", Type = typeof(Control))]
[TemplatePart(Name = "PART_ContentPresenter", Type = typeof(ContentPresenter))]
public class DesignerItem : ContentControl, ISelectable, IGroupable
{
#region ID
private Guid id;
public Guid ID
{
get { return id; }
}
#endregion
#region ParentID
public Guid ParentID
{
get { return (Guid)GetValue(ParentIDProperty); }
set { SetValue(ParentIDProperty, value); }
}
public static readonly DependencyProperty ParentIDProperty = DependencyProperty.Register("ParentID", typeof(Guid), typeof(DesignerItem));
#endregion
#region IsGroup
public bool IsGroup
{
get { return (bool)GetValue(IsGroupProperty); }
set { SetValue(IsGroupProperty, value); }
}
public static readonly DependencyProperty IsGroupProperty =
DependencyProperty.Register("IsGroup", typeof(bool), typeof(DesignerItem));
#endregion
#region IsSelected Property
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected",
typeof(bool),
typeof(DesignerItem),
new FrameworkPropertyMetadata(false));
#endregion
#region DragThumbTemplate Property
// can be used to replace the default template for the DragThumb
public static readonly DependencyProperty DragThumbTemplateProperty =
DependencyProperty.RegisterAttached("DragThumbTemplate", typeof(ControlTemplate), typeof(DesignerItem));
public static ControlTemplate GetDragThumbTemplate(UIElement element)
{
return (ControlTemplate)element.GetValue(DragThumbTemplateProperty);
}
public static void SetDragThumbTemplate(UIElement element, ControlTemplate value)
{
element.SetValue(DragThumbTemplateProperty, value);
}
#endregion
#region ConnectorDecoratorTemplate Property
// can be used to replace the default template for the ConnectorDecorator
public static readonly DependencyProperty ConnectorDecoratorTemplateProperty =
DependencyProperty.RegisterAttached("ConnectorDecoratorTemplate", typeof(ControlTemplate), typeof(DesignerItem));
public static ControlTemplate GetConnectorDecoratorTemplate(UIElement element)
{
return (ControlTemplate)element.GetValue(ConnectorDecoratorTemplateProperty);
}
public static void SetConnectorDecoratorTemplate(UIElement element, ControlTemplate value)
{
element.SetValue(ConnectorDecoratorTemplateProperty, value);
}
#endregion
#region IsDragConnectionOver
// while drag connection procedure is ongoing and the mouse moves over
// this item this value is true; if true the ConnectorDecorator is triggered
// to be visible, see template
public bool IsDragConnectionOver
{
get { return (bool)GetValue(IsDragConnectionOverProperty); }
set { SetValue(IsDragConnectionOverProperty, value); }
}
public static readonly DependencyProperty IsDragConnectionOverProperty =
DependencyProperty.Register("IsDragConnectionOver",
typeof(bool),
typeof(DesignerItem),
new FrameworkPropertyMetadata(false));
#endregion
static DesignerItem()
{
// set the key to reference the style for this control
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
typeof(DesignerItem), new FrameworkPropertyMetadata(typeof(DesignerItem)));
}
public DesignerItem(Guid id)
{
this.id = id;
this.Loaded += new RoutedEventHandler(DesignerItem_Loaded);
}
public DesignerItem()
: this(Guid.NewGuid())
{
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
base.OnPreviewMouseDown(e);
DesignerCanvas designer = VisualTreeHelper.GetParent(this) as DesignerCanvas;
// update selection
if (designer != null)
{
if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
if (this.IsSelected)
{
designer.SelectionService.RemoveFromSelection(this);
}
else
{
designer.SelectionService.AddToSelection(this);
}
else if (!this.IsSelected)
{
designer.SelectionService.SelectItem(this);
}
Focus();
}
e.Handled = false;
}
void DesignerItem_Loaded(object sender, RoutedEventArgs e)
{
if (base.Template != null)
{
ContentPresenter contentPresenter =
this.Template.FindName("PART_ContentPresenter", this) as ContentPresenter;
if (contentPresenter != null)
{
UIElement contentVisual = VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement;
if (contentVisual != null)
{
DragThumb thumb = this.Template.FindName("PART_DragThumb", this) as DragThumb;
if (thumb != null)
{
ControlTemplate template =
DesignerItem.GetDragThumbTemplate(contentVisual) as ControlTemplate;
if (template != null)
thumb.Template = template;
}
}
}
}
}
}
}