/
ModelListViewItem.cs
182 lines (144 loc) · 5.91 KB
/
ModelListViewItem.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
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
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MVC.WPF {
public class ModelListViewItem : UserControl {
public ModelListView ParentList;
protected IModelItem _data;
public IModelItem DataSource {
set {
if (_data != null)
_data.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(DataSource_PropertyChanged);
_data = value;
LoadSourceData();
_data.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(DataSource_PropertyChanged);
}
get {
return _data;
}
}
public new object Content {
get {
return contentLabel.Content;
}
set {
contentLabel.Content = value;
}
}
protected Rectangle BackgroundRectangle = new Rectangle();
protected Rectangle Highlight = new Rectangle();
protected Label contentLabel = new Label();
public ModelListViewItem() {
this.Padding = new Thickness(1);
this.Background = new SolidColorBrush(Colors.Transparent);
BackgroundRectangle.RadiusX = 5;
BackgroundRectangle.RadiusY = BackgroundRectangle.RadiusX;
Highlight.RadiusX = 5;
Highlight.RadiusY = BackgroundRectangle.RadiusX;
Highlight.Stroke = Brushes.Black;
Highlight.StrokeThickness = 1;
Highlight.Opacity = 0;
contentLabel.Background = new SolidColorBrush(Colors.Transparent);
contentLabel.Padding = new Thickness(5);
contentLabel.Margin = new Thickness(0);
contentLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
contentLabel.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
// contentLabel.BorderBrush = new SolidColorBrush(Colors.Black);
// contentLabel.BorderThickness = new System.Windows.Thickness(1);
Grid grid = new Grid();
grid.Children.Add(BackgroundRectangle);
grid.Children.Add(Highlight);
grid.Children.Add(contentLabel);
base.Content = grid;
this.MouseDown += new MouseButtonEventHandler(UserControl_MouseLeftButtonDown);
this.MouseEnter += new MouseEventHandler(ModelListViewItem_MouseEnter);
this.MouseLeave += new MouseEventHandler(ModelListViewItem_MouseLeave);
}
void ModelListViewItem_MouseLeave(object sender, MouseEventArgs e) {
Highlight.Opacity = 0;
}
void ModelListViewItem_MouseEnter(object sender, MouseEventArgs e) {
Highlight.Opacity = 0.5;
}
protected void setSelected() {
if (DataSource.IsSelected) {
BackgroundRectangle.Fill = SelectedBackgroundColor;
changeForegroundColor(SelectedTextColor);
} else {
BackgroundRectangle.Fill = BackgroundColor;
changeForegroundColor(TextColor);
}
}
protected virtual void DataSource_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
switch (e.PropertyName) {
case "IsSelected":
setSelected();
break;
}
}
protected virtual void changeForegroundColor(Brush brush) {
contentLabel.Foreground = brush;
}
protected virtual Brush SelectedTextColor {
get {
return new SolidColorBrush(Colors.Black);
SolidColorBrush brush = SelectedBackgroundColor as SolidColorBrush;
Color color = brush.Color;
int total = color.R + color.G + color.B;
if (total > 400)
return new SolidColorBrush(Colors.Black);
else
return new SolidColorBrush(Colors.White);
}
}
protected virtual Brush TextColor {
get {
return new SolidColorBrush(Colors.Black);
}
}
protected virtual Brush SelectedBackgroundColor {
get {
return new SolidColorBrush(Colors.DarkBlue);
}
}
protected virtual Brush BackgroundColor {
get {
return new SolidColorBrush(Colors.Transparent);
}
}
public bool IsSelected {
get {
return DataSource.IsSelected;
}
set {
DataSource.IsSelected = value;
}
}
public virtual ModelListViewItem CreateItem() {
return new ModelListViewItem();
}
protected void TopLevelGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
}
protected void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
if (ParentList != null) {
ParentList.ItemWasClicked(this);
} else {
IsSelected = !IsSelected;
}
}
public virtual void LoadSourceData() {
BackgroundRectangle.Fill = BackgroundColor;
this.ToolTip = _data.ToolTip;
setSelected();
LoadContent();
}
protected virtual void LoadContent() {
Content = DataSource;
}
protected System.Windows.Media.Color convertColor(System.Drawing.Color color) {
return Color.FromArgb(color.A, color.R, color.G, color.B);
}
}
}