![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 private Color getColorFromString(string colorString)
2 {
3 Type colorType = (typeof(System.Windows.Media.Colors));
4 if (colorType.GetProperty(colorString) != null)
5 {
6 object color = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);
7 if (color != null)
8 {
9 return (Color)color;
10 }
11 }
12 throw new InvalidCastException("Color not defined");
13 }
14
15 private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
16 {
17 try
18 {
19 rg1.SetValue(Rectangle.FillProperty, new SolidColorBrush(getColorFromString(txtColorBegin.Text)));
20
21 rg2.SetValue(Rectangle.FillProperty, new SolidColorBrush(getColorFromString(txtColorEnd.Text)));
22 }
23 catch (InvalidCastException)
24 {
25
26 }
27 }