一,动态生成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. IntelliJ IDEA 中使用region代码折叠

    我使用的版本为2018.3,如下图: 选中要折叠的代码,使用快捷键:Ctrl+Alt+T,打开Surround With菜单,点击region...endregion Comments项(红框),如下 ...

  2. 基础篇-密码文件.pgpass

    密码文件包含字段 host:port:dbname:user:password port和dbname可以使用*号作通配符 在*nix系统中 ~/.pgpass 在windows系统中 %APPDAT ...

  3. hp惠普工作站 安装ESXI 从U盘启动黑屏问题

    [背景] 公司新买了一套hp z238 workstation需要安装esxi,我按照以前的方法制作好U盘后插入从U盘引导,显示屏一直黑屏,这个问题困扰了我一天....在网上找了好久无果 第二天终于在 ...

  4. JS高级学习历程-15

    昨天内容回顾 面向对象的私有成员.静态成员 私有成员:在构造函数里边定义局部变量,就是私有成员. 静态成员:在js里边,函数就是对象,可以通过给函数对象声明成员方式声明静态成员. 原型继承 关键字:p ...

  5. 基于Vue的省市区三级联动插件

    官网地址:https://distpicker.uine.org/ 安装: npm install v-distpicker --save 局部注册: import VDistpicker from ...

  6. Java微信公众平台开发(十一)--微信JSSDK中Config配置

    JSSDK曾经引爆前端以及后端的工程师,其魔性的力量毋庸置疑,在我们的技术眼里它的实现原理和根本是不能够被改变的,这篇文章就不对其js的实现做任何评价和解说了(因为我也不是很懂,哈哈),这里要说的是它 ...

  7. meta标签的一些用法

    meta是html语言head区的一个辅助性标签.几乎所有的网页里,我们可以看到类似下面这段的html代码: <head> <meta http-equiv="conten ...

  8. 【转】ibatis 中使用select top #pagesize# * from tablename

    ibatis中使用select top #num# * from tableName出现错误.由于初次用ibatis还不知道在它里边拼写SQL语句的一些规则,导致一些自认为很平常的SQL语句,在它这里 ...

  9. angularjs e2e测试初步学习(一)

    e2e测试是从用户角度出发,认为整个系统都是一个黑盒,只有UI暴露出来. angularjs的测试框架是采用protractor. 1.创建文件 首先创建一个项目文件夹test,然后再创建两个文件,一 ...

  10. hadoop完全分布式模式搭建和hive安装

    简介 Hadoop是用来处理大数据集合的分布式存储计算基础架构.可以使用一种简单的编程模式,通过多台计算机构成的集群,分布式处理大数据集.hadoop作为底层,其生态环境很丰富. hadoop基础包括 ...