一,动态生成Grid

 public static Grid CreateGrid(List<T_METER> List)
{
var g = new Grid();
if (null == List) return g;
g.HorizontalAlignment = HorizontalAlignment.Stretch;
g.VerticalAlignment = VerticalAlignment.Bottom;
var meters = List.OrderBy(i => (string.IsNullOrEmpty(i.F_ORDER_NO) ? : Convert.ToInt32(i.F_ORDER_NO)));
var count = meters.Count();
var sdr = List.FirstOrDefault(i => i.F_EQT_TYPE.ToUpper().Equals("C"));
var exteneral = List.FirstOrDefault(i => i.F_EQT_TYPE.ToUpper().Equals("E"));
#region 生成列
for (int i = ; i < ; i++)
{
var rd = new RowDefinition();
if (i == )
rd.Height = new GridLength();
else
rd.Height = new GridLength();
g.RowDefinitions.Add(rd);
}
for (int i = ; i < count; i++)
{
var cd = new ColumnDefinition();
if (i == )
cd.Width = new GridLength();
else if (i == )
{
if (sdr != null)
cd.Width = new GridLength();
else
cd.Width = new GridLength();
}
else if (i == )
{
if (exteneral != null)
cd.Width = new GridLength();
else
cd.Width = new GridLength();
}
else
cd.Width = new GridLength();
g.ColumnDefinitions.Add(cd);
}
#endregion #region 数据绑定
for (int i = ; i < count; i++)
{ var meter = meters.ElementAt(i);
var namepath = string.Format("Objs[{0}].MeterName", i);
var statepath = string.Format("Objs[{0}].SwitchState.Value", i);
if (meter.F_EQT_TYPE.Equals("D"))
{
var tranLeft = new TranLeft();
tranLeft.SetBinding(TranLeft.MeterNameProperty, new Binding(namepath));
tranLeft.SetBinding(TranLeft.SwitchStateProperty, new Binding(statepath));
tranLeft.SetValue(Grid.RowProperty, );
tranLeft.SetValue(Grid.ColumnProperty, i);
tranLeft.Width = ;
g.Children.Add(tranLeft);
}
else if (meter.F_EQT_TYPE.Equals("B"))
{
var tran1 = new Tran1();
tran1.SetBinding(Tran1.MeterNameProperty, new Binding(namepath));
tran1.SetBinding(Tran1.SwitchStateProperty, new Binding(statepath));
tran1.SetValue(Grid.RowProperty, );
tran1.SetValue(Grid.ColumnProperty, i);
tran1.Width = ;
g.Children.Add(tran1);
}
else if (meter.F_EQT_TYPE.Equals("C"))
{
var tran2 = new Tran2();
tran2.SetBinding(Tran2.MeterNameProperty, new Binding(namepath));
tran2.SetBinding(Tran2.SwitchStateProperty, new Binding(statepath));
tran2.SetValue(Grid.RowProperty, );
tran2.SetValue(Grid.ColumnProperty, i);
tran2.Width = ;
g.Children.Add(tran2);
}
else if (meter.F_EQT_TYPE.Equals("E"))
{
var external = new External();
external.SetBinding(External.MeterNameProperty, new Binding(namepath));
external.SetBinding(External.SwitchStateProperty, new Binding(statepath));
external.SetValue(Grid.RowProperty, );
external.SetValue(Grid.ColumnProperty, i);
external.Width = ;
g.Children.Add(external);
}
else
{
var m = new Meter();
m.SetBinding(Meter.MeterNameProperty, new Binding(namepath));
m.SetBinding(Meter.SwitchStateProperty, new Binding(statepath));
m.SetValue(Grid.RowProperty, );
m.SetValue(Grid.ColumnProperty, i);
g.Children.Add(m);
} var uaCell = new CustomCell();
uaCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FUA.Value", i)));
uaCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FUA.IsAlarm", i)));
uaCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FUA.IsButton", i)));
uaCell.SetValue(Grid.RowProperty, );
uaCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(uaCell); var ubCell = new CustomCell();
ubCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FUB.Value", i)));
ubCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FUB.IsAlarm", i)));
ubCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FUB.IsButton", i)));
ubCell.SetValue(Grid.RowProperty, );
ubCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(ubCell); var ucCell = new CustomCell();
ucCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FUC.Value", i)));
ucCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FUC.IsAlarm", i)));
ucCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FUC.IsButton", i)));
ucCell.SetValue(Grid.RowProperty, );
ucCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(ucCell); var iaCell = new CustomCell();
iaCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FIA.Value", i)));
iaCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FIA.IsAlarm", i)));
iaCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FIA.IsButton", i)));
iaCell.SetValue(Grid.RowProperty, );
iaCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(iaCell); var ibCell = new CustomCell();
ibCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FIB.Value", i)));
ibCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FIB.IsAlarm", i)));
ibCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FIB.IsButton", i)));
ibCell.SetValue(Grid.RowProperty, );
ibCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(ibCell); var icCell = new CustomCell();
icCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].FIC.Value", i)));
icCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].FIC.IsAlarm", i)));
icCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].FIC.IsButton", i)));
icCell.SetValue(Grid.RowProperty, );
icCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(icCell); var alarmTypeCell = new CustomCell();
alarmTypeCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].AlarmType.Value", i)));
alarmTypeCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].AlarmType.IsAlarm", i)));
alarmTypeCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].AlarmType.IsButton", i)));
alarmTypeCell.SetValue(Grid.RowProperty, );
alarmTypeCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(alarmTypeCell); var meterAlarmCell = new CustomCell();
meterAlarmCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].MeterAlarm.Value", i)));
meterAlarmCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].MeterAlarm.IsAlarm", i)));
meterAlarmCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].MeterAlarm.IsButton", i)));
meterAlarmCell.SetValue(Grid.RowProperty, );
meterAlarmCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(meterAlarmCell); var powerCell = new CustomCell();
powerCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].Power.Value", i)));
powerCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].Power.IsAlarm", i)));
powerCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].Power.IsButton", i)));
powerCell.SetValue(Grid.RowProperty, );
powerCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(powerCell); var trendCell = new CustomCell();
trendCell.SetBinding(CustomCell.ValueProperty, new Binding(string.Format("Objs[{0}].Trend.Value", i)));
trendCell.SetBinding(CustomCell.IsAlarmProperty, new Binding(string.Format("Objs[{0}].Trend.IsAlarm", i)));
trendCell.SetBinding(CustomCell.IsButtonProperty, new Binding(string.Format("Objs[{0}].Trend.IsButton", i)));
trendCell.SetBinding(CustomCell.BuildIdProperty, new Binding(string.Format("Objs[{0}].BuildId", i)));
trendCell.SetBinding(CustomCell.MeterIdProperty, new Binding(string.Format("Objs[{0}].MeterId", i)));
trendCell.SetValue(Grid.RowProperty, );
trendCell.SetValue(Grid.ColumnProperty, i);
g.Children.Add(trendCell);
}
#endregion
GridHelper.ShowBorder(g);
return g;
}

二,Grid边框操作

 public class GridHelper
{
////A0B3C6
public static SolidColorBrush _BorderBrush = new SolidColorBrush(ConvertFromString("#FFA0B3C6"));
public static double _BorderThickness = ; /// <summary>
/// 生成边框
/// </summary>
public static void ShowBorder(Grid gd)
{
if (gd == null)
{
return;
} clearBorder(gd); SolidColorBrush BorderColorBrush = _BorderBrush;
double BorderThickness = _BorderThickness; CreateBorder(gd, BorderColorBrush, BorderThickness); } /// <summary>
/// 生成边框
/// </summary>
public static void ShowBorder(Grid gd, SolidColorBrush BorderColorBrush, double BorderThickness)
{
if (gd == null)
{
return;
} clearBorder(gd); CreateBorder(gd, BorderColorBrush, BorderThickness); } private static void clearBorder(Grid gd)
{
for (int i = gd.Children.Count - ; i >= ; i--)
{
if (gd.Children[i].ToString() == "System.Windows.Controls.Border")
{
Border re = gd.Children[i] as Border;
if (re.Tag == "autoBorder")
{
gd.Children.RemoveAt(i);
}
}
} } private static void CreateBorder(Grid gd, SolidColorBrush BorderBrush, double BorderThickness)
{
for (int i = ; i < gd.RowDefinitions.Count; i++)
{
for (int j = ; j < gd.ColumnDefinitions.Count; j++)
{
Border boIn = new Border(); boIn.BorderBrush = BorderBrush;
if (i == )
{
boIn.BorderThickness = new Thickness(, , , BorderThickness);
}
else
{
if (j == )
boIn.BorderThickness = new Thickness(BorderThickness, , BorderThickness, BorderThickness);
else
boIn.BorderThickness = new Thickness(, , BorderThickness, BorderThickness);
}
boIn.SetValue(Grid.ColumnProperty, j);
boIn.SetValue(Grid.RowProperty, i);
boIn.Tag = "autoBorder";
gd.Children.Add(boIn);
}
}
} public static Color ConvertFromString(string htmlColor)
{
htmlColor = htmlColor.Replace("#", "");
byte a = 0xff, r = , g = , b = ;
switch (htmlColor.Length)
{
case :
r = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
break;
case :
a = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
r = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
break;
case :
r = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
break;
case :
a = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
r = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(, ), System.Globalization.NumberStyles.HexNumber);
break;
}
return Color.FromArgb(a, r, g, b);
} }

Silverlight日记:动态操作Grid的更多相关文章

  1. Silverlight StoryBoard 动态切换ImageSource

    Silverlight StoryBoard 动态切换ImageSource <StackPanel Grid.Row="1" Orientation="Horiz ...

  2. IE7中使用Jquery动态操作name问题

    问题:IE7中无法使用Jquery动态操作页面元素的name属性. 在项目中有出现问题,某些客户的机器偶尔会有,后台取不到前台的数据值. 然开发和测试环境总是不能重现问题.坑爹之处就在于此,不能重现就 ...

  3. 完全使用一组 DSL 来操作 Grid 控件

    最近尝试了一下将 XtraGrid 的初始化工作封装成内部 DSL,例如一个普通的基础数据的增删改查操作的代码会像下面这样: public partial class UserForm : XtraF ...

  4. MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作

    一.getMapper()接口 解析:getMapper()接口 IDept.class定义一个接口, 挂载一个没有实现的方法,特殊之处,借楼任何方法,必须和小配置中id属性是一致的 通过代理:生成接 ...

  5. js实现动态操作table

     本章案例为通过js,动态操作table,实现在单页面进行增删改查的操作. 简要案例如下: <%@ page language="java" contentType=&quo ...

  6. 如何动态修改grid的列名

    有这样的需求,搜索时候会选择搜索类型,每种搜索类型展示的列名不一样 如何动态修改grid的列名 效果图:点击bColumn页面切换成bColumn 实现思路:通过grid的reconfigure方法, ...

  7. unity3d动态操作组件

    利用范型,动态操作组件(添加或删除) e.AddComponent<CubeTranslate> ();//动态添加组件 Destroy (e.GetComponent<CubeTr ...

  8. silverlight子窗体操作数据库后刷新父窗体

    silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...

  9. C#--动态操作DataTable

    C#动态操作DataTable(新增行.列.查询行.列等) 方法一:动态创建一个DataTable ,并为其添加数据 public void CreateTable()        {        ...

随机推荐

  1. [openjudge] 2797最短前缀 Trie

    描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的字串是: "c", "ca", "car&q ...

  2. hdu 5033 buiding(单调栈)

    hdu 5033 buiding(单调栈) 某年某月某天,马特去了一个小镇.这个小镇如此狭窄,以至于他可以把小镇当作一个枢纽.在镇上有一些摩天大楼,其中一栋位于xi,高度为hi.所有的摩天大楼位于不同 ...

  3. IT兄弟连 JavaWeb教程 使用AJAX发送GET请求并获取响应

    GET请求用于获取数据,有时候我们需要获取的数据需要通过"查询参数"进行定位,在这种情况下,我们会将查询参数追加到URL的末尾,令服务器解析. 使用Ajax发送GET请求非常简单, ...

  4. 剑指Offer的学习笔记(C#篇)-- 构建乘积数组

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  5. 8.聚集函数 ---SQL

    一.AVG()函数 A VG()通过对表中行数计数并计算其列值之和,求得该列的平均值.A VG()可用来返回所有列的平均值,也可以用来返回特定列或行的平均值. 警告:只用于单个列 AVG()只能用来确 ...

  6. jdbc查询

    import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.BeanProperty ...

  7. spring boot 使用hibernate validator 验证service

    不在controller中验证,而是在service中验证. spring boot 默认使用的就是hibernate validator,存在于pom的spring-boot-starter-web ...

  8. (转)Linux基础知识学习

    Linux基础知识学习 原文:http://blog.csdn.net/ye_wei_yang/article/details/52777499 一.Linux的磁盘分区及目录 Linux的配置是通过 ...

  9. 将JWT与Spring Security OAuth结合使用

    1.概述 在本教程中,我们将讨论如何使用Spring Security OAuth2实现来使用JSON Web令牌. 我们还将继续构建此OAuth系列的上一篇文章. 2. Maven配置 首先,我们需 ...

  10. List和set集合:交集、差集、合集的区别retainAll,removeAll、addAll

    set .list集合的交集(retainAll).差集(removeAll)是没有区别的都是一样的. set .list集合的合集addAll是有区别的:set可以去重复:list不去重复 publ ...