所有的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 }