一,动态生成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. bzoj 4788: [CERC2016]Bipartite Blanket【hall定理+状压】

    考虑当前合法的一个点集s,如果他合法,那么一定有一个完备匹配的点集包含这个点集,也就是两边都满足hall定理的话这两边拼起来的点集也满足要求 所以分别状压两边点集用hall定理转移判断当前点集是否合法 ...

  2. 洛谷P1044 栈(Catalan数)

    P1044 栈 题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要 ...

  3. -cogs1247. [Nescafé29] 穿越七色虹

    1247. [Nescafé29] 穿越七色虹 ★   输入文件:rainbow.in   输出文件:rainbow.out   简单对比时间限制:5 s   内存限制:128 MB [背景] 在Ne ...

  4. uoj#279. 【UTR #2】题目交流通道(容斥+数数)

    传送门 先考虑无解的情况,为以下几种:\(dis_{i,j}+dis_{j,k}<dis_{i,k}\),\(dis_{i,i}\neq 0\),\(dis_{i,j}\neq dis_{j,i ...

  5. Vue2中实现微信分享支付功能

    Vue2中实现微信分享支付功能  近期做了一个微信公众号前后分离项目,前端采用Vue2开发,后端SpringBoot,今天迫不及待的来给大家分享一下这次在开发中遇到的一些坑以及解决办法. 在这里,一些 ...

  6. JS实现简单时钟效果

    老师上课需要我们做一个时钟的小作业 ,我把它放在上面记录一下啦 表盘和时针我都是用的背景图的形式,然后绝对定位,通过调整left和top确定时针.分针.秒针的位置,transform-origin设置 ...

  7. PJzhang:搜索引擎高级语法与渗透测试

    猫宁!!! 参考链接: https://www.freebuf.com/articles/network/169601.html https://www.jianshu.com/p/f8062e2cc ...

  8. 有关xerosploit运行报错问题的有效解决方案

    [安装xerosploit]安装xerosploit的步骤如下,我是将xerosploit直接克隆到了根目录下(使用“cd /”到达根目录) git clone https://github.com/ ...

  9. datastream解析

    在EOS的eosiolib模块中有一个datasteam.hpp文件,它几乎实现了所有类型对字节流的转换,是一个非常强大的工具类,在这里对它的做一个简单的提取,也加强一下自己对它的理解.在下面的工程中 ...

  10. ORA-01950:表空间“USERS”中无权限

    ORA-01950:表空间“USERS”中无权限 解决方案: A)确认给用户授权了resource角色 B)取消限制 ALTER USER "HCCPMS" QUOTA UNLIM ...