Silverlight日记:动态操作Grid
一,动态生成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的更多相关文章
- Silverlight StoryBoard 动态切换ImageSource
Silverlight StoryBoard 动态切换ImageSource <StackPanel Grid.Row="1" Orientation="Horiz ...
- IE7中使用Jquery动态操作name问题
问题:IE7中无法使用Jquery动态操作页面元素的name属性. 在项目中有出现问题,某些客户的机器偶尔会有,后台取不到前台的数据值. 然开发和测试环境总是不能重现问题.坑爹之处就在于此,不能重现就 ...
- 完全使用一组 DSL 来操作 Grid 控件
最近尝试了一下将 XtraGrid 的初始化工作封装成内部 DSL,例如一个普通的基础数据的增删改查操作的代码会像下面这样: public partial class UserForm : XtraF ...
- MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作
一.getMapper()接口 解析:getMapper()接口 IDept.class定义一个接口, 挂载一个没有实现的方法,特殊之处,借楼任何方法,必须和小配置中id属性是一致的 通过代理:生成接 ...
- js实现动态操作table
本章案例为通过js,动态操作table,实现在单页面进行增删改查的操作. 简要案例如下: <%@ page language="java" contentType=&quo ...
- 如何动态修改grid的列名
有这样的需求,搜索时候会选择搜索类型,每种搜索类型展示的列名不一样 如何动态修改grid的列名 效果图:点击bColumn页面切换成bColumn 实现思路:通过grid的reconfigure方法, ...
- unity3d动态操作组件
利用范型,动态操作组件(添加或删除) e.AddComponent<CubeTranslate> ();//动态添加组件 Destroy (e.GetComponent<CubeTr ...
- silverlight子窗体操作数据库后刷新父窗体
silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...
- C#--动态操作DataTable
C#动态操作DataTable(新增行.列.查询行.列等) 方法一:动态创建一个DataTable ,并为其添加数据 public void CreateTable() { ...
随机推荐
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- 运行Spark程序的几种模式
一. local 模式 -- 所有程序都运行在一个JVM中,主要用于开发时测试 无需开启任何服务,可直接运行 ./bin/run-example 或 ./bin/spark-submit 如: ...
- [转] 【iOS基础知识】之判断NSString是否为整数、浮点数
//判断是否为整形: - (BOOL)isPureInt:(NSString*)string{ NSScanner* scan = [NSScannerscannerWithString:string ...
- [Android]Android性能优化
安卓性能优化 性能优化的几大考虑 Mobile Context 资源受限 + 内存,普遍较小,512MB很常见,开发者的机器一般比用户的机器高端 + CPU,核心少,运算能力没有全开 + GPU,上传 ...
- Codeforces 140D(贪心)
要点 跟大家打acm的策略一样,为了做更多的题数肯定做最简单的题目,为了罚时更少肯定从易到难做 虽然有个12:00之限不同于往常比赛,但细想还是要从易到难贪:做这些题的总时间肯定是不变的,只是顺序可变 ...
- Netty(3)Time protocol
本节介绍TIME协议.该协议与前边的discard协议和echo协议的不同点在于:1.服务端主动发送消息给到客户端,所以需要channelActive()方法.2.发送的消息是4个字节的int3.不接 ...
- (转)Nginx的https配置记录以及http强制跳转到https的方法梳理
Nginx的https配置记录以及http强制跳转到https的方法梳理 原文:http://www.cnblogs.com/kevingrace/p/6187072.html 一.Nginx安装(略 ...
- Spark Mllib里决策树回归分析使用.rootMeanSquaredError方法计算出以RMSE来评估模型的准确率(图文详解)
不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型 ...
- dubbo rest返回值异常Incompatible types: declared root type
2018-08-28 17:26:02,208 [http-bio-9090-exec-1][][][][][] ERROR com.wjs.member.plugin.intercepter.Ser ...
- .NET CORE IIS 500.21
最近遇到的.NET CORE 500.21的错误 官方解决方案地址:https://docs.microsoft.com/en-us/dynamics-nav/troubleshooting-http ...