Silverlight日记:动态操作Grid

一,动态生成Grid

  1 public static Grid CreateGrid(List<T_METER> List)
  2         {
  3             var g = new Grid();
  4             if (null == List) return g;
  5             g.HorizontalAlignment = HorizontalAlignment.Stretch;
  6             g.VerticalAlignment = VerticalAlignment.Bottom;
  7             var meters = List.OrderBy(i => (string.IsNullOrEmpty(i.F_ORDER_NO) ? 0 : Convert.ToInt32(i.F_ORDER_NO)));
  8             var count = meters.Count();
  9             var sdr = List.FirstOrDefault(i => i.F_EQT_TYPE.ToUpper().Equals("C"));
 10             var exteneral = List.FirstOrDefault(i => i.F_EQT_TYPE.ToUpper().Equals("E"));
 11             #region 生成列
 12             for (int i = 0; i < 11; i++)
 13             {
 14                 var rd = new RowDefinition();
 15                 if (i == 0)
 16                     rd.Height = new GridLength(440);
 17                 else
 18                     rd.Height = new GridLength(30);
 19                 g.RowDefinitions.Add(rd);
 20             }
 21             for (int i = 0; i < count; i++)
 22             {
 23                 var cd = new ColumnDefinition();
 24                 if (i == 0)
 25                     cd.Width = new GridLength(120);
 26                 else if (i == 1)
 27                 {
 28                     if (sdr != null)
 29                         cd.Width = new GridLength(150);
 30                     else
 31                         cd.Width = new GridLength(90);
 32                 }
 33                 else if (i == 2)
 34                 {
 35                     if (exteneral != null)
 36                         cd.Width = new GridLength(98);
 37                     else
 38                         cd.Width = new GridLength(71);
 39                 }
 40                 else
 41                     cd.Width = new GridLength(71);
 42                 g.ColumnDefinitions.Add(cd);
 43             }
 44             #endregion
 45 
 46             #region 数据绑定
 47             for (int i = 0; i < count; i++)
 48             {
 49 
 50                 var meter = meters.ElementAt(i);
 51                 var namepath = string.Format("Objs[{0}].MeterName", i);
 52                 var statepath = string.Format("Objs[{0}].SwitchState.Value", i);
 53                 if (meter.F_EQT_TYPE.Equals("D"))
 54                 {
 55                     var tranLeft = new TranLeft();
 56                     tranLeft.SetBinding(TranLeft.MeterNameProperty, new Binding(namepath));
 57                     tranLeft.SetBinding(TranLeft.SwitchStateProperty, new Binding(statepath));
 58                     tranLeft.SetValue(Grid.RowProperty, 0);
 59                     tranLeft.SetValue(Grid.ColumnProperty, i);
 60                     tranLeft.Width = 120;
 61                     g.Children.Add(tranLeft);
 62                 }
 63                 else if (meter.F_EQT_TYPE.Equals("B"))
 64                 {
 65                     var tran1 = new Tran1();
 66                     tran1.SetBinding(Tran1.MeterNameProperty, new Binding(namepath));
 67                     tran1.SetBinding(Tran1.SwitchStateProperty, new Binding(statepath));
 68                     tran1.SetValue(Grid.RowProperty, 0);
 69                     tran1.SetValue(Grid.ColumnProperty, i);
 70                     tran1.Width = 90;
 71                     g.Children.Add(tran1);
 72                 }
 73                 else if (meter.F_EQT_TYPE.Equals("C"))
 74                 {
 75                     var tran2 = new Tran2();
 76                     tran2.SetBinding(Tran2.MeterNameProperty, new Binding(namepath));
 77                     tran2.SetBinding(Tran2.SwitchStateProperty, new Binding(statepath));
 78                     tran2.SetValue(Grid.RowProperty, 0);
 79                     tran2.SetValue(Grid.ColumnProperty, i);
 80                     tran2.Width = 150;
 81                     g.Children.Add(tran2);
 82                 }
 83                 else if (meter.F_EQT_TYPE.Equals("E"))
 84                 {
 85                     var external = new External();
 86                     external.SetBinding(External.MeterNameProperty, new Binding(namepath));
 87                     external.SetBinding(External.SwitchStateProperty, new Binding(statepath));
 88                     external.SetValue(Grid.RowProperty, 0);
 89                     external.SetValue(Grid.ColumnProperty, i);
 90                     external.Width = 98;
 91                     g.Children.Add(external);
 92                 }
 93                 else
 94                 {
 95                     var m = new Meter();
 96                     m.SetBinding(Meter.MeterNameProperty, new Binding(namepath));
 97                     m.SetBinding(Meter.SwitchStateProperty, new Binding(statepath));
 98                     m.SetValue(Grid.RowProperty, 0);
 99                     m.SetValue(Grid.ColumnProperty, i);
100                     g.Children.Add(m);
101                 }
102 
103                 var uaCell = new CustomCell();
104                 uaCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FUA.Value", i)));
105                 uaCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FUA.IsAlarm", i)));
106                 uaCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FUA.IsButton", i)));
107                 uaCell.SetValue(Grid.RowProperty, 1);
108                 uaCell.SetValue(Grid.ColumnProperty, i);
109                 g.Children.Add(uaCell);
110 
111                 var ubCell = new CustomCell();
112                 ubCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FUB.Value", i)));
113                 ubCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FUB.IsAlarm", i)));
114                 ubCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FUB.IsButton", i)));
115                 ubCell.SetValue(Grid.RowProperty, 2);
116                 ubCell.SetValue(Grid.ColumnProperty, i);
117                 g.Children.Add(ubCell);
118 
119                 var ucCell = new CustomCell();
120                 ucCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FUC.Value", i)));
121                 ucCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FUC.IsAlarm", i)));
122                 ucCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FUC.IsButton", i)));
123                 ucCell.SetValue(Grid.RowProperty, 3);
124                 ucCell.SetValue(Grid.ColumnProperty, i);
125                 g.Children.Add(ucCell);
126 
127 
128                 var iaCell = new CustomCell();
129                 iaCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FIA.Value", i)));
130                 iaCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FIA.IsAlarm", i)));
131                 iaCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FIA.IsButton", i)));
132                 iaCell.SetValue(Grid.RowProperty, 4);
133                 iaCell.SetValue(Grid.ColumnProperty, i);
134                 g.Children.Add(iaCell);
135 
136                 var ibCell = new CustomCell();
137                 ibCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FIB.Value", i)));
138                 ibCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FIB.IsAlarm", i)));
139                 ibCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FIB.IsButton", i)));
140                 ibCell.SetValue(Grid.RowProperty, 5);
141                 ibCell.SetValue(Grid.ColumnProperty, i);
142                 g.Children.Add(ibCell);
143 
144                 var icCell = new CustomCell();
145                 icCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FIC.Value", i)));
146                 icCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FIC.IsAlarm", i)));
147                 icCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FIC.IsButton", i)));
148                 icCell.SetValue(Grid.RowProperty, 6);
149                 icCell.SetValue(Grid.ColumnProperty, i);
150                 g.Children.Add(icCell);
151 
152                 var alarmTypeCell = new CustomCell();
153                 alarmTypeCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].AlarmType.Value", i)));
154                 alarmTypeCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].AlarmType.IsAlarm", i)));
155                 alarmTypeCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].AlarmType.IsButton", i)));
156                 alarmTypeCell.SetValue(Grid.RowProperty, 7);
157                 alarmTypeCell.SetValue(Grid.ColumnProperty, i);
158                 g.Children.Add(alarmTypeCell);
159 
160 
161                 var meterAlarmCell = new CustomCell();
162                 meterAlarmCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].MeterAlarm.Value", i)));
163                 meterAlarmCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].MeterAlarm.IsAlarm", i)));
164                 meterAlarmCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].MeterAlarm.IsButton", i)));
165                 meterAlarmCell.SetValue(Grid.RowProperty, 8);
166                 meterAlarmCell.SetValue(Grid.ColumnProperty, i);
167                 g.Children.Add(meterAlarmCell);
168 
169                 var powerCell = new CustomCell();
170                 powerCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].Power.Value", i)));
171                 powerCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].Power.IsAlarm", i)));
172                 powerCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].Power.IsButton", i)));
173                 powerCell.SetValue(Grid.RowProperty, 9);
174                 powerCell.SetValue(Grid.ColumnProperty, i);
175                 g.Children.Add(powerCell);
176 
177                 var trendCell = new CustomCell();
178                 trendCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].Trend.Value", i)));
179                 trendCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].Trend.IsAlarm", i)));
180                 trendCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].Trend.IsButton", i)));
181                 trendCell.SetBinding(CustomCell.BuildIdProperty, new Binding(string.Format("Objs[{0}].BuildId", i)));
182                 trendCell.SetBinding(CustomCell.MeterIdProperty, new Binding(string.Format("Objs[{0}].MeterId", i)));
183                 trendCell.SetValue(Grid.RowProperty, 10);
184                 trendCell.SetValue(Grid.ColumnProperty, i);
185                 g.Children.Add(trendCell);
186             }
187             #endregion
188             GridHelper.ShowBorder(g);
189             return g;
190         }

二,Grid边框操作

  1 public class GridHelper
  2     {
  3         ////A0B3C6
  4         public static SolidColorBrush _BorderBrush = new SolidColorBrush(ConvertFromString("#FFA0B3C6"));
  5         public static double _BorderThickness = 1;
  6 
  7         /// <summary>
  8         /// 生成边框
  9         /// </summary>
 10         public static void ShowBorder(Grid gd)
 11         {
 12             if (gd == null)
 13             {
 14                 return;
 15             }
 16 
 17             clearBorder(gd);
 18 
 19             SolidColorBrush BorderColorBrush = _BorderBrush;
 20             double BorderThickness = _BorderThickness;
 21 
 22             CreateBorder(gd, BorderColorBrush, BorderThickness);
 23 
 24         }
 25 
 26         /// <summary>
 27         /// 生成边框
 28         /// </summary>
 29         public static void ShowBorder(Grid gd, SolidColorBrush BorderColorBrush, double BorderThickness)
 30         {
 31             if (gd == null)
 32             {
 33                 return;
 34             }
 35 
 36             clearBorder(gd);
 37 
 38             CreateBorder(gd, BorderColorBrush, BorderThickness);
 39 
 40         }
 41 
 42         private static void clearBorder(Grid gd)
 43         {
 44             for (int i = gd.Children.Count - 1; i >= 0; i--)
 45             {
 46                 if (gd.Children[i].ToString() == "System.Windows.Controls.Border")
 47                 {
 48                     Border re = gd.Children[i] as Border;
 49                     if (re.Tag == "autoBorder")
 50                     {
 51                         gd.Children.RemoveAt(i);
 52                     }
 53                 }
 54             }
 55 
 56         }
 57 
 58         private static void CreateBorder(Grid gd, SolidColorBrush BorderBrush, double BorderThickness)
 59         {
 60             for (int i = 0; i < gd.RowDefinitions.Count; i++)
 61             {
 62                 for (int j = 0; j < gd.ColumnDefinitions.Count; j++)
 63                 {
 64                     Border boIn = new Border();
 65 
 66                     boIn.BorderBrush = BorderBrush;
 67                     if (i == 0)
 68                     {
 69                         boIn.BorderThickness = new Thickness(0, 0, 0, BorderThickness);
 70                     }
 71                     else
 72                     {
 73                         if (j == 0)                        
 74                             boIn.BorderThickness = new Thickness(BorderThickness, 0, BorderThickness, BorderThickness);                        
 75                         else                        
 76                             boIn.BorderThickness = new Thickness(0, 0, BorderThickness, BorderThickness);                        
 77                     }
 78                     boIn.SetValue(Grid.ColumnProperty, j);
 79                     boIn.SetValue(Grid.RowProperty, i);
 80                     boIn.Tag = "autoBorder";
 81                     gd.Children.Add(boIn);
 82                 }
 83             }
 84         }
 85 
 86 
 87         public static Color ConvertFromString(string htmlColor)
 88         {
 89             htmlColor = htmlColor.Replace("#", "");
 90             byte a = 0xff, r = 0, g = 0, b = 0;
 91             switch (htmlColor.Length)
 92             {
 93                 case 3:
 94                     r = byte.Parse(htmlColor.Substring(0, 1), System.Globalization.NumberStyles.HexNumber);
 95                     g = byte.Parse(htmlColor.Substring(1, 1), System.Globalization.NumberStyles.HexNumber);
 96                     b = byte.Parse(htmlColor.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
 97                     break;
 98                 case 4:
 99                     a = byte.Parse(htmlColor.Substring(0, 1), System.Globalization.NumberStyles.HexNumber);
100                     r = byte.Parse(htmlColor.Substring(1, 1), System.Globalization.NumberStyles.HexNumber);
101                     g = byte.Parse(htmlColor.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
102                     b = byte.Parse(htmlColor.Substring(3, 1), System.Globalization.NumberStyles.HexNumber);
103                     break;
104                 case 6:
105                     r = byte.Parse(htmlColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
106                     g = byte.Parse(htmlColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
107                     b = byte.Parse(htmlColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
108                     break;
109                 case 8:
110                     a = byte.Parse(htmlColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
111                     r = byte.Parse(htmlColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
112                     g = byte.Parse(htmlColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
113                     b = byte.Parse(htmlColor.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
114                     break;
115             }
116             return Color.FromArgb(a, r, g, b);
117         }
118 
119     }

 

posted @ 2014-08-27 15:25  Code & Life  阅读(296)  评论(0编辑  收藏  举报