-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGridUtils.cs
145 lines (127 loc) · 5.02 KB
/
GridUtils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace ChatBubbles
{
public class GridUtils
{
#region RowDefinitions attached property
/// <summary>
/// Identified the RowDefinitions attached property
/// </summary>
public static readonly DependencyProperty RowDefinitionsProperty =
DependencyProperty.RegisterAttached("RowDefinitions", typeof(string), typeof(GridUtils),
new PropertyMetadata("", new PropertyChangedCallback(OnRowDefinitionsPropertyChanged)));
/// <summary>
/// Gets the value of the RowDefinitions property
/// </summary>
public static string GetRowDefinitions(DependencyObject d)
{
return (string)d.GetValue(RowDefinitionsProperty);
}
/// <summary>
/// Sets the value of the RowDefinitions property
/// </summary>
public static void SetRowDefinitions(DependencyObject d, string value)
{
d.SetValue(RowDefinitionsProperty, value);
}
/// <summary>
/// Handles property changed event for the RowDefinitions property, constructing
/// the required RowDefinitions elements on the grid which this property is attached to.
/// </summary>
private static void OnRowDefinitionsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Grid targetGrid = d as Grid;
// construct the required row definitions
targetGrid.RowDefinitions.Clear();
string rowDefs = e.NewValue as string;
var rowDefArray = rowDefs.Split(',');
foreach (string rowDefinition in rowDefArray)
{
if (rowDefinition.Trim() == "")
{
targetGrid.RowDefinitions.Add(new RowDefinition());
}
else
{
targetGrid.RowDefinitions.Add(new RowDefinition()
{
Height = ParseLength(rowDefinition)
});
}
}
}
#endregion
#region ColumnDefinitions attached property
/// <summary>
/// Identifies the ColumnDefinitions attached property
/// </summary>
public static readonly DependencyProperty ColumnDefinitionsProperty =
DependencyProperty.RegisterAttached("ColumnDefinitions", typeof(string), typeof(GridUtils),
new PropertyMetadata("", new PropertyChangedCallback(OnColumnDefinitionsPropertyChanged)));
/// <summary>
/// Gets the value of the ColumnDefinitions property
/// </summary>
public static string GetColumnDefinitions(DependencyObject d)
{
return (string)d.GetValue(ColumnDefinitionsProperty);
}
/// <summary>
/// Sets the value of the ColumnDefinitions property
/// </summary>
public static void SetColumnDefinitions(DependencyObject d, string value)
{
d.SetValue(ColumnDefinitionsProperty, value);
}
/// <summary>
/// Handles property changed event for the ColumnDefinitions property, constructing
/// the required ColumnDefinitions elements on the grid which this property is attached to.
/// </summary>
private static void OnColumnDefinitionsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Grid targetGrid = d as Grid;
// construct the required column definitions
targetGrid.ColumnDefinitions.Clear();
string columnDefs = e.NewValue as string;
var columnDefArray = columnDefs.Split(',');
foreach (string columnDefinition in columnDefArray)
{
if (columnDefinition.Trim() == "")
{
targetGrid.ColumnDefinitions.Add(new ColumnDefinition());
}
else
{
targetGrid.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = ParseLength(columnDefinition)
});
}
}
}
#endregion
/// <summary>
/// Parses a string to create a GridLength
/// </summary>
private static GridLength ParseLength(string length)
{
length = length.Trim();
if (length.ToLowerInvariant().Equals("auto"))
{
return new GridLength(0, GridUnitType.Auto);
}
else if (length.Contains("*"))
{
length = length.Replace("*", "");
if (string.IsNullOrEmpty(length)) length = "1";
return new GridLength(double.Parse(length), GridUnitType.Star);
}
return new GridLength(double.Parse(length), GridUnitType.Pixel);
}
}
}