1: [TemplatePart(Name = TilePad.RootElementName, Type = typeof(Grid))]
2: public sealed class TilePad : Control
3: {
4: public event EventHandler TileClick;
5:
6: private const string RootElementName = "RootElement";
7: public Grid RootElement { get; set; }
8:
9: #region Columns
10:
11: public static readonly DependencyProperty ColumnsProperty =
12: DependencyProperty.Register("Columns", typeof(int), typeof(TilePad), new PropertyMetadata(1));
13:
14: public int Columns
15: {
16: get { return (int)GetValue(ColumnsProperty); }
17: set { SetValue(ColumnsProperty, value); }
18: }
19:
20: #endregion
21:
22: #region Rows
23:
24: public static readonly DependencyProperty RowsProperty =
25: DependencyProperty.Register("Rows", typeof(int), typeof(TilePad), new PropertyMetadata(1));
26:
27: public int Rows
28: {
29: get { return (int)GetValue(RowsProperty); }
30: set { SetValue(RowsProperty, value); }
31: }
32:
33: #endregion
34:
35: #region NormalTileStyle
36:
37: public static readonly DependencyProperty NormalTileStyleProperty =
38: DependencyProperty.Register("NormalTileStyle", typeof(Style), typeof(TilePad), new PropertyMetadata(null));
39:
40: public Style NormalTileStyle
41: {
42: get { return (Style)GetValue(NormalTileStyleProperty); }
43: set { SetValue(NormalTileStyleProperty, value); }
44: }
45:
46: #endregion
47:
48: #region HoverTileStyle
49:
50: public static readonly DependencyProperty HoverTileStyleProperty =
51: DependencyProperty.Register("HoverTileStyle", typeof(Style), typeof(TilePad), new PropertyMetadata(null));
52:
53: public Style HoverTileStyle
54: {
55: get { return (Style)GetValue(HoverTileStyleProperty); }
56: set { SetValue(HoverTileStyleProperty, value); }
57: }
58:
59: #endregion
60:
61: public TilePad()
62: {
63: this.DefaultStyleKey = typeof(TilePad);
64: }
65:
66: protected override void OnApplyTemplate()
67: {
68: this.RootElement = this.GetTemplateChild(TilePad.RootElementName) as Grid;
69: this.BuildTiles();
70: base.OnApplyTemplate();
71: }
72:
73: private void BuildTiles()
74: {
75: if (this.RootElement != null)
76: {
77: this.RootElement.Children.Clear();
78:
79: this.RootElement.ColumnDefinitions.Clear();
80: foreach(var column in from i in Enumerable.Range(0, this.Columns)
81: select new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) })
82: this.RootElement.ColumnDefinitions.Add(column);
83:
84: this.RootElement.RowDefinitions.Clear();
85: foreach (var row in from i in Enumerable.Range(0, this.Rows)
86: select new RowDefinition { Height = new GridLength(1, GridUnitType.Star) })
87: this.RootElement.RowDefinitions.Add(row);
88:
89: for (int x = 0; x < this.Columns; x++)
90: {
91: for (int y = 0; y < this.Rows; y++)
92: {
93: Rectangle tile = new Rectangle();
94: tile.Style = this.NormalTileStyle;
95: Grid.SetColumn(tile, x);
96: Grid.SetRow(tile, y);
97: tile.PointerReleased += HandlePointerReleased;
98: tile.PointerEntered += HandleToggleStyle;
99: tile.PointerExited += HandleToggleStyle;
100: this.RootElement.Children.Add(tile);
101: }
102: }
103: }
104: }
105:
106: private void HandlePointerReleased(object sender, PointerRoutedEventArgs e)
107: {
108: Rectangle tile = sender as Rectangle;
109:
110: if (tile != null && this.TileClick != null)
111: this.TileClick(tile, EventArgs.Empty);
112: }
113:
114: private void HandleToggleStyle(object sender, PointerRoutedEventArgs e)
115: {
116: Rectangle tile = sender as Rectangle;
117:
118: if (tile != null)
119: {
120: if (Object.Equals(tile.Style, this.NormalTileStyle))
121: tile.Style = this.HoverTileStyle;
122: else if (Object.Equals(tile.Style, this.HoverTileStyle))
123: tile.Style = this.NormalTileStyle;
124: }
125: }
126: }