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() { ...
随机推荐
- -cogs1247. [Nescafé29] 穿越七色虹
1247. [Nescafé29] 穿越七色虹 ★ 输入文件:rainbow.in 输出文件:rainbow.out 简单对比时间限制:5 s 内存限制:128 MB [背景] 在Ne ...
- VM Fusion配置静态IP和物理机通讯
Vm虚拟机在WIndow系统上和物理机进行通讯很方便,但是在Mac上简直跟吃了屎一样难用的要死,物理机断了网以后还不能和虚拟机通讯, 如果在windows上做开发,也是简直和吃了屎一样,难用的要屎,这 ...
- Unity 播放的声音比声音文件小很多-AudioListener-AudioClip
今天做愤怒的小鸟时,播放的时候非常非常小,怎么也查不到原因,就去问群里的大佬.原来, 播放音乐的方法: AudioSource.PlayClipAtPoint(audioclip, transform ...
- Javascript 给table动态增、删除行
操作 HTML DOM Table 对象 即可 http://www.runoob.com/jsref/dom-obj-table.html 动态给一个元素焦点,用focus()方法
- Glassfish 设置时区
对于Glassfish domain 或者instance下,某个日志的时区不对,前提是系统时区争取. 可以尝试通过如下命令查看jvm 时区设置 asadmin list-jvm-options 如果 ...
- xml布局文件
https://blog.csdn.net/u013475386/article/details/44339035 gravity写在容器中中 layout_gravity写在控件中 layout_m ...
- Python模块之 sys
# sys模块是与python解释器交互的一个接口 import sys print(sys.argv) # 命令行参数list,第一个元素是程序本身路径 # (第一个元素就是执行文件的时候,写在py ...
- Restful 1 -- REST、DRF(View源码解读、APIView源码解读)及框架实现
一.REST 1.什么是编程? 数据结构和算法的结合 2.什么是REST? - url用来唯一定位资源,http请求方式来区分用户行为 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下 ...
- java 通用查询框架Querydsl 简介
Querydsl 是一个通用的查询框架,专注于通过JavaAPI构建类型安全的SQL查询说说Querydsl的优势吧: 1. Querydsl支持代码自动完成,因为才纯Java API编写查询,因此主 ...
- Sql server 查询指定时间区间工作日数、休息日数等日期操作
1.查询指定时间区间的工作日 这个主要难点是法定节假日,国家的法定节假日每年都不一样,还涉及到调休,所以我们设计一个假日表.主要字段有年份,类型(是否调休),假期日期.如下: CREATE TABLE ...