XML生成XAMl扩展
所有的WPF控件列为枚举
代码如:
1 public enum ControlType
2 {
3 Window_Resources,
4 Page_Resources,
5 Grid,
6 StackPanel,
7 DataGrid,
8 TreeView,
9 TreeViewItem,
10 Button,
11 WrapPanel,
12 Button_CommandParameter,
13 MultiBinding,
14 Binding,
15 TextBox,
16 CheckBox,
17 RadioButton,
18 DataGrid_Columns,
19 DataGridTemplateColumn,
20 DataGridTemplateColumn_CellTemplate,
21 DataTemplate,
22 Border,
23 DataGridTextColumn,
24 ColumnDefinition,
25 RowDefinition,
26 Grid_RowDefinitions,
27 Grid_ColumnDefinitions
28 }
由于枚举不支持小数点.,所以把小数点.改为下划线_
扩展代码为:
1 public static class XAMLExtend
2 {
3 #region 定义命名空间
4
5 public static XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
6 public static XNamespace x = "http://schemas.microsoft.com/winfx/2006/xaml";
7 public static XNamespace mc = "http://schemas.openxmlformats.org/markup-compatibility/2006";
8 public static XNamespace d = "http://schemas.microsoft.com/expression/blend/2008";
9 public static XNamespace local = "clr-namespace:GenesysInfo.MainApp;assembly=GenesysInfo.MainApp";
10 public static XNamespace dic = "clr-namespace:System.Collections.Specialized;assembly=System";
11 public static XNamespace sys = "clr-namespace:System;assembly=mscorlib";
12 public static XNamespace bll = "clr-namespace:GenesysInfo.MainApp.Helper;assembly=GenesysInfo.MainApp";
13 public static XNamespace c = "clr-namespace:GenesysInfo.MainApp.CommandCenter;assembly=GenesysInfo.MainApp";
14 public static XNamespace ctr = "clr-namespace:GenesysInfo.Windows.Controls;assembly=GenesysInfo.ComponentModel";
15 public static XNamespace service = "clr-namespace:GenesysInfo.Data.Service;assembly=GenesysInfo.Data.Service";
16 public static XNamespace data = "clr-namespace:GenesysInfo.Data;assembly=GenesysInfo.Data";
17
18 #endregion
19
20 /// <summary>
21 /// 添加窗体页面命名空间
22 /// </summary>
23 /// <param name="xElement"></param>
24 public static void SetNamespace(this XElement xElement)
25 {
26 xElement.SetAttributeValue(XNamespace.Xmlns + "mc", mc);
27 xElement.SetAttributeValue(XNamespace.Xmlns + "x", x);
28 xElement.SetAttributeValue(XNamespace.Xmlns + "d", d);
29 xElement.SetAttributeValue(XNamespace.Xmlns + "local", local);
30 xElement.SetAttributeValue(XNamespace.Xmlns + "dic", dic);
31 xElement.SetAttributeValue(XNamespace.Xmlns + "sys", sys);
32 xElement.SetAttributeValue(XNamespace.Xmlns + "bll", bll);
33 xElement.SetAttributeValue(XNamespace.Xmlns + "c", c);
34 xElement.SetAttributeValue(XNamespace.Xmlns + "ctr", ctr);
35 xElement.SetAttributeValue(XNamespace.Xmlns + "service", service);
36 xElement.SetAttributeValue(XNamespace.Xmlns + "data", data);
37 }
38
39 /// <summary>
40 /// 添加默认属性
41 /// </summary>
42 /// <param name="xElement"></param>
43 public static void SetDefaultProperty(this XElement xElement)
44 {
45 xElement.Add(new XAttribute(mc + "Ignorable", "d"));
46 xElement.Add(new XAttribute(d + "DesignHeight", "768"));
47 xElement.Add(new XAttribute(d + "DesignWidth", "1024"));
48 }
49
50 /// <summary>
51 /// 设置页面窗体标题
52 /// </summary>
53 /// <param name="xElement"></param>
54 /// <param name="title"></param>
55 public static XElement SetTitle(this XElement xElement, object title)
56 {
57 if (title!=null)
58 xElement.Add(new XAttribute(x + "Title",Convert.ToString(title)));
59 return xElement;
60 }
61
62 /// <summary>
63 /// 设置窗体、页面、控件的名称
64 /// </summary>
65 /// <param name="xElement"></param>
66 /// <param name="name"></param>
67 public static XElement SetName(this XElement xElement, object name)
68 {
69 if (name!=null)
70 xElement.Add(new XAttribute(x + "Name", Convert.ToString(name)));
71 return xElement;
72 }
73
74 /// <summary>
75 /// 设置控件宽高度
76 /// </summary>
77 /// <param name="xElement"></param>
78 /// <param name="width"></param>
79 /// <param name="height"></param>
80 public static XElement SetWH(this XElement xElement,object width, object height)
81 {
82 if (width!=null)
83 xElement.Add(new XAttribute("Width",Convert.ToString(width)));
84
85 if (height!=null)
86 xElement.Add(new XAttribute("Height", Convert.ToString(height)));
87
88 return xElement;
89 }
90
91 /// <summary>
92 /// 设置控件宽度
93 /// </summary>
94 /// <param name="xElement"></param>
95 /// <param name="width"></param>
96 public static XElement SetW(this XElement xElement, object width)
97 {
98 xElement.SetWH(width, null);
99 return xElement;
100 }
101
102 /// <summary>
103 /// 设置控件高度
104 /// </summary>
105 /// <param name="xElement"></param>
106 /// <param name="height"></param>
107 public static XElement SetH(this XElement xElement, object height)
108 {
109 xElement.SetWH(null, height);
110 return xElement;
111 }
112
113 /// <summary>
114 /// 创建控件
115 /// </summary>
116 /// <param name="controlType"></param>
117 /// <returns></returns>
118 public static XElement Create(this ControlType controlType)
119 {
120 return new XElement(xmlns + controlType.GetControlName().Replace("_", "."));
121 }
122
123 /// <summary>
124 /// 在parent中创建子控件
125 /// </summary>
126 /// <param name="parent"></param>
127 /// <param name="controlType"></param>
128 /// <returns></returns>
129 public static XElement AddChildControl(this XElement parent, ControlType controlType)
130 {
131 XElement newXE = Create(controlType);
132 parent.Add(newXE);
133 return newXE;
134 }
135
136 /// <summary>
137 /// 取得控件类型的名称
138 /// </summary>
139 /// <param name="controlType"></param>
140 /// <returns></returns>
141 public static string GetControlName(this ControlType controlType)
142 {
143 return Enum.GetName(typeof(ControlType), controlType);
144 }
145
146 public static XElement SetMinHeight(this XElement xElement, object minHeight)
147 {
148 xElement.Add(new XAttribute("MinHeight",Convert.ToString(minHeight)));
149 return xElement;
150 }
151
152 public static XElement SetMinWidth(this XElement xElement, object minWidth)
153 {
154 xElement.Add(new XAttribute("MinWidth", Convert.ToString(minWidth)));
155 return xElement;
156 }
157
158 public static XElement SetMaxWidth(this XElement xElement, object maxWidth)
159 {
160 xElement.Add(new XAttribute("MaxWidth", Convert.ToString(maxWidth)));
161 return xElement;
162 }
163
164 public static XElement SetMargin(this XElement xElement, object value)
165 {
166 xElement.Add(new XAttribute("Margin", Convert.ToString(value)));
167 return xElement;
168 }
169
170 public static XElement SetContent(this XElement xElement, object text)
171 {
172 xElement.Add(new XAttribute("Content", Convert.ToString(text)));
173 return xElement;
174 }
175
176 public static XElement SetConverter(this XElement xElement, object text)
177 {
178 xElement.Add(new XAttribute("Converter", Convert.ToString(text)));
179 return xElement;
180 }
181
182 public static XElement SetConverterParameter(this XElement xElement, object text)
183 {
184 xElement.Add(new XAttribute("ConverterParameter", Convert.ToString(text)));
185 return xElement;
186 }
187
188 public static XElement SetPath(this XElement xElement, object text)
189 {
190 xElement.Add(new XAttribute("Path", Convert.ToString(text)));
191 return xElement;
192 }
193
194 public static XElement SetOrientation(this XElement xElement, object text)
195 {
196 xElement.Add(new XAttribute("Orientation", Convert.ToString(text)));
197 return xElement;
198 }
199
200 public static XElement SetHorizontalAlignment(this XElement xElement, object text)
201 {
202 xElement.Add(new XAttribute("HorizontalAlignment", Convert.ToString(text)));
203 return xElement;
204 }
205
206 public static XElement SetText(this XElement xElement, object text)
207 {
208 xElement.Add(new XAttribute("Text", Convert.ToString(text)));
209 return xElement;
210 }
211
212 public static XElement SetIsReadOnly(this XElement xElement, object text)
213 {
214 xElement.Add(new XAttribute("IsReadOnly", Convert.ToString(text)));
215 return xElement;
216 }
217
218 public static XElement SetGroupName(this XElement xElement, object text)
219 {
220 xElement.Add(new XAttribute("GroupName", Convert.ToString(text)));
221 return xElement;
222 }
223
224 public static XElement SetTag(this XElement xElement, object text)
225 {
226 xElement.Add(new XAttribute("Tag", Convert.ToString(text)));
227 return xElement;
228 }
229
230 public static XElement SetStyle(this XElement xElement, object text)
231 {
232 xElement.Add(new XAttribute("Style", Convert.ToString(text)));
233 return xElement;
234 }
235
236 public static XElement SetAutoGenerateColumns(this XElement xElement, object text)
237 {
238 xElement.Add(new XAttribute("AutoGenerateColumns", Convert.ToString(text)));
239 return xElement;
240 }
241
242 public static XElement SetCanUserAddRows(this XElement xElement, object text)
243 {
244 xElement.Add(new XAttribute("CanUserAddRows", Convert.ToString(text)));
245 return xElement;
246 }
247
248 public static XElement SetCommand(this XElement xElement, object text)
249 {
250 xElement.Add(new XAttribute("Command", Convert.ToString(text)));
251 return xElement;
252 }
253
254 public static XElement SetHeader(this XElement xElement, object text)
255 {
256 xElement.Add(new XAttribute("Header", Convert.ToString(text)));
257 return xElement;
258 }
259
260 public static XElement SetVisibility(this XElement xElement, object text)
261 {
262 xElement.Add(new XAttribute("Visibility", Convert.ToString(text)));
263 return xElement;
264 }
265
266 public static XElement SetSortDirection(this XElement xElement, object text)
267 {
268 xElement.Add(new XAttribute("SortDirection", Convert.ToString(text)));
269 return xElement;
270 }
271
272 public static XElement SetBinding(this XElement xElement, object text)
273 {
274 xElement.Add(new XAttribute("Binding", Convert.ToString(text)));
275 return xElement;
276 }
277
278 public static XElement SetGrid_Column(this XElement xElement, object text)
279 {
280 xElement.Add(new XAttribute("Grid.Column", Convert.ToString(text)));
281 return xElement;
282 }
283
284 public static XElement SetGrid_Row(this XElement xElement, object text)
285 {
286 xElement.Add(new XAttribute("Grid.Row", Convert.ToString(text)));
287 return xElement;
288 }
XML生成XAMl扩展的更多相关文章
- Spring的Xml和JavaConfig 扩展你选哪一个?
引言 上一篇文章我们有怎么介绍到如何通过XML的形式来定义Spring的扩展<Spring面试高频题如何:自定义XML schema 扩展>,好多人都在吐槽现在都什么年代了,xml还有人再 ...
- Difference Between XML and XAML.
XML, or Extensible Markup Language, is a subset of the more complex SGML (Standard Generalized Mark ...
- 通过xml生成word文档
Xml生成word总结 使用xml生成word的基本步骤在<使用xslt转化xml数据形成word文档导出.doc>中说明比较清楚了.但是其中的细节并未说到,因此自己折腾了两天总算成功了. ...
- 关于XML(可扩展标记语言)的基础知识与写法
XML(Extensible Markup Language) HTML:超文本标记语言,主要用来展示 XML:可扩展标记语言,用来做数据传输XML特点:1.树状结构,有且只有一个根2.标签名自定 ...
- C#操作Xml树的扩展类
本文提供一个操作Xml树的扩展类,与将xml字符串直接映射成实体对象的使用方法,供大家参考,学习. 下面附上源码 using System; using System.Collections.Gene ...
- 根据xml生成相应的对象类
根据xml生成相应的class对象,听起来很难其实很简单,用xsd.exe就能办到 打开vs 命令行运行xsd.exe 你的xml文件地址 空格/outputdir:存放xsd的地址 ok,这是生成了 ...
- spring mvc: xml生成
spring mvc: xml生成 准备: javax.xml.bind.annotation.XmlElement; javax.xml.bind.annotation.XmlRootElement ...
- XML简介——可扩展标记语言(Extensible Markup Language)
(What) XML是什么? XML指可扩展标记语言(Extensible Markup Language) 1. XML是一种标记语言,类似HTML. 2. XML具有自我描述性 3. XML ...
- x:Static , StaticResource 和DynamicResource等XAML 扩展用法
原文:x:Static , StaticResource 和DynamicResource等XAML 扩展用法 前提: <system:String x:Key="{Component ...
随机推荐
- javaMail实现收发邮件(五)
控制台打印出的内容,我们无法阅读,其实,让我们自己来解析一封复杂的邮件是很不容易的,邮件里面格式.规范复杂得很.不过,我们所用的浏览器内置了解析各种数据类型的数据处理模块,我们只需要在把数据流传输给浏 ...
- 【CPU微架构设计】分布式多端口(4写2读)寄存器堆设计
寄存器堆(Register File)是微处理的关键部件之一.寄存器堆往往具有多个读写端口,其中写端口往往与多个处理单元相对应.传统的方法是使用集中式寄存器堆,即一个集中式寄存器堆匹配N个处理单元.随 ...
- Centos 7 Ntop 流量分析 安装
Centos 6 安装 Ntop:https://www.cnblogs.com/weijie0717/p/4886314.html 一.安装 1.添加EPEL 仓库 # yum install ep ...
- Scrapy简单入门及实例讲解
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以 ...
- css动画特效
<html> <head> <meta charset="utf-8" /> <title>6种css3鼠标滑过动画效果</t ...
- Spring基础篇——Spring的AOP切面编程
一 基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...
- c#dev gridview 设置隔行换色等
1:禁止gridview编辑 2:隔行换色 介绍一些常用的gridcontrol设置. 1.设置隔行变色.首先设置显示隔行变色,步骤:OptionsView-->EnableAppearance ...
- Servlet中的转发与重定向
Sevlet 的转发与重定向都可以使得浏览器指向另一个资源文件,但它们的运行机制不相同. 一.Servlet的转发 有两种方式获得转发对象(RequestDispathcer): HttpServle ...
- Kudu基本操作及概念
Kudu: 针对 Apache Hadoop 平台而开发的列式存储管理器. 使用场景: 适用于那些既有随机访问,也有批量数据扫描的复合场景. 高计算量的场景. 使用了高性能的存 ...
- LR回放https协议脚本失败:[GENERAL_MSG_CAT_SSL_ERROR]connect to host "XXX" failed:[10054] Connection reset by peer [MsgId:MERR-27780]
Loadrunner默认发送是通过sockets(将http转换为sockets)发送的,而sockets默认SSL的版本为SSL2和SSL3.HTTPS协议录制的脚本以SSL3版本回放时会使sock ...