public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { List <CustomPropertyDescriptor> list = new List <CustomPropertyDescriptor>(); PropertyDescriptorCollection list2 = TypeDescriptor.GetProperties(selectObject, attributes); if (list2 == null || list2.Count <= 0) { return(list2); } Dictionary <String, DataFieldItem> dic = DataFieldItem.GetFields(selectObject.GetType()); foreach (PropertyDescriptor item in list2) { CustomPropertyDescriptor entity = new CustomPropertyDescriptor(selectObject, item); if (dic.ContainsKey(item.Name)) { entity.SetCategory("数据"); entity.DataField = dic[item.Name]; } else { entity.SetCategory("其它"); } list.Add(entity); } return(new PropertyDescriptorCollection(list.ToArray())); }
private List <DataFieldItem> GetFieldItems(List <string[]> ItemInfo) { List <DataFieldItem> items = new List <DataFieldItem>(); for (int i = 0; i < ItemInfo.Count; i++) { DataFieldItem df = new DataFieldItem(); df.PropertyDisplayName = ItemInfo[i][0]; df.PropertyName = ItemInfo[i][1]; df.ValueMember = ItemInfo[i][2]; df.CType = (DataFieldItem.ControlType)(int.Parse(ItemInfo[i][3])); df.Requited = Convert.ToBoolean(int.Parse(ItemInfo[i][4])); items.Add(df); } return(items); }
private GridLength GetRowHeight(DataFieldItem dfItem) { ControlType cType = dfItem.CType; switch (cType) { case ControlType.Label: case ControlType.TextBox: case ControlType.Combobox: case ControlType.LookUp: return(new GridLength(30)); case ControlType.Remark: return(new GridLength(30, GridUnitType.Auto)); default: return(new GridLength(30)); } }
private void ShowSampleData() { List <string[]> FInfo = new List <string[]>(); FInfo.Add(new string[] { "字段1", "OrderCode", "OrderCode", "1" }); FInfo.Add(new string[] { "字段2", "OrderStates", "OrderStates", "1" }); FInfo.Add(new string[] { "字段3", "CreateUser", "CreateUser", "1" }); FInfo.Add(new string[] { "字段4", "CreateDate", "CreateDate", "1" }); for (int i = 0; i < FInfo.Count; i++) { DataFieldItem df = new DataFieldItem(); df.PropertyDisplayName = FInfo[i][0]; df.Name = FInfo[i][1]; df.PropertyName = FInfo[i][2]; df.CType = (ControlType)(int.Parse(FInfo[i][3])); Items.Add(df); } this.InitForm(); }
/// <summary> /// 创建结构体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <param name="address"></param> /// <param name="canPostfix"></param> /// <returns></returns> public static Struct Create <TEntity>(TEntity entity, UInt32 address, Boolean canPostfix) where TEntity : EntityBase <TEntity>, new() { Type type = typeof(TEntity); // 类名作为结构体名字 String name = type.Name; // 检查是否已存在 //UInt32 id = Struct.GetStructID(name); //if (id != Bytes.BadAddress) //{ // KernelWin.WriteLine("结构体{0}(ID={1:X}h)已存在!", name, id); // return Struct.FindStructByID(id); //} Struct st = Struct.FindStructByName(name); //if (st != null) //{ // KernelWin.WriteLine("结构体{0}(ID={1:X}h)已存在!", name, st.ID); // return st; //} Int32 n = 0; String name2 = name; while (st == null) { st = Struct.Create(name2, Bytes.BadAddress, false); n++; if (st == null) { if (n > 10 || !canPostfix) { throw new Exception("创建结构体失败!"); } name2 = name + "_" + n; } } Dictionary <String, DataFieldItem> dic = DataFieldItem.GetFields(type); foreach (DataFieldItem item in dic.Values) { name = item.Property.Name; DataType dt; UInt32 size = 0; if (item.Attribute.RefType == null) { #region 普通成员 if (item.Property.PropertyType == typeof(String)) { dt = DataType.ASCI; if (!String.IsNullOrEmpty(item.Attribute.SizeField)) { size = Convert.ToUInt32(entity[item.Attribute.SizeField]); } if (size <= 0) { size = (UInt32)item.Attribute.Size; } } else if (item.Property.PropertyType == typeof(Byte[])) { // 字节数组也可以当作字符串一样处理 dt = DataType.ASCI; if (!String.IsNullOrEmpty(item.Attribute.SizeField)) { size = Convert.ToUInt32(entity[item.Attribute.SizeField]); } if (size <= 0) { size = (UInt32)item.Attribute.Size; } } else if (item.Property.PropertyType == typeof(Byte)) { dt = DataType.BYTE; size = 1; } else if (item.Property.PropertyType == typeof(Int16)) { dt = DataType.WORD; size = 2; } else if (item.Property.PropertyType == typeof(Int32)) { dt = DataType.DWRD; size = 4; } else { throw new Exception("不支持的类型:" + type.Name); } //KernelWin.WriteLine("创建普通成员:{0} {1} {2} ", name, dt, size); st.Add(name, dt, size); #endregion } else { #region 引用成员 if (item.Property.PropertyType == typeof(Int16)) { dt = DataType.WORD | DataType.F0OFF; size = 2; } else if (item.Property.PropertyType == typeof(Int32)) { dt = DataType.DWRD | DataType.F0OFF; size = 4; } else { throw new Exception("不支持的类型:" + type.Name); } UInt32 addr = 0; switch (item.Attribute.RefKind) { case RefKinds.Virtual: addr = 0; break; case RefKinds.Relative: addr = address; break; case RefKinds.Auto: // 先直接取地址 addr = Convert.ToUInt32(item.Property.GetValue(entity, null)); // 如果小于基址,可能是相对地址 if (addr < entity.Info.ImageBase && addr > 0) { addr = address; } else { addr = 0; } break; case RefKinds.Absolute: throw new Exception("不支持的类型:" + item.Attribute.RefKind); } //KernelWin.WriteLine("创建引用成员:{0} {1} 0x{2:X} {3}", name, dt, addr, size); st.Add(name, Bytes.BadAddress, dt, addr, size); #endregion } } return(st); }
/// <summary> /// 创建并标识结构体 /// </summary> /// <typeparam name="TEntity">结构体实体类型</typeparam> /// <param name="entity">结构体数据实体</param> /// <param name="address">结构体基地址,引用类型的成员可能需要该地址作为相对地址</param> /// <param name="canPostfix">当名称已存在时,是否允许使用后缀</param> /// <returns></returns> public static Struct Make <TEntity>(TEntity entity, UInt32 address, Boolean canPostfix) where TEntity : EntityBase <TEntity>, new() { Int32 addr = (Int32)(entity.Address + entity.Info.ImageBase); Struct st = VBStruct.Create <TEntity>(entity, address, canPostfix); if (st == null) { throw new Exception(String.Format("为类型{0}创建结构体失败!", typeof(TEntity))); } Bytes.MakeNameAnyway((UInt32)addr, typeof(TEntity).Name); //KernelWin.WriteLine("MakeStruct: 0x{0:X8} {1:X}h {2}", addr, (Int32)EntityBase<TEntity>.ObjectSize, st.Name); //Bytes.MakeStruct(addr, (Int32)EntityBase<TEntity>.ObjectSize, st.Name); MakeStruct <TEntity>(addr, st); // 处理结构体成员中的字符串 Dictionary <String, DataFieldItem> dic = DataFieldItem.GetFields(typeof(TEntity)); foreach (DataFieldItem item in dic.Values) { if (item.Attribute.RefType != typeof(String)) { continue; } // 先直接取地址 Int32 temp = Convert.ToInt32(item.Property.GetValue(entity, null)); if (temp <= 0) { continue; } UInt32 address2 = (UInt32)temp; switch (item.Attribute.RefKind) { case RefKinds.Virtual: break; case RefKinds.Relative: // 相对,加上前面的结构体基地址 address2 += (UInt32)entity.Address; break; case RefKinds.Auto: // 如果小于基址,可能是相对地址 if (address2 < entity.Info.ImageBase && address2 > 0) { address2 += (UInt32)entity.Address; } break; case RefKinds.Absolute: throw new Exception("不支持的类型:" + item.Attribute.RefKind); } if (address2 <= 0) { continue; } // 标为字符串 //Bytes.MakeUnknown(address2, 0); Bytes.MakeAscii(address2, (Int32)Bytes.BadAddress, StringType.C); } return(st); }
private FrameworkElement CreateControl(DataFieldItem dfItem) { ControlType cType = dfItem.CType; Binding binding = new Binding(); binding.Path = new PropertyPath(dfItem.PropertyName); binding.Mode = BindingMode.TwoWay; binding.Converter = new CommonConvert(dfItem); FrameworkElement c = null; switch (cType) { case ControlType.Label: TextBlock tbx = new TextBlock(); if (dfItem.ReferenceDataInfo != null) { binding.Path = new PropertyPath(dfItem.ReferenceDataInfo.ReferencedMember + ".Text"); } tbx.SetBinding(TextBlock.TextProperty, binding); c = tbx; break; case ControlType.TextBox: TextBox tb = new TextBox(); tb.SetBinding(TextBox.TextProperty, binding); tb.IsReadOnly = (IsReadOnly || dfItem.IsReadOnly); tb.MaxLength = dfItem.MaxLength; if (dfItem.DataType.ToLower() == "decimal" || dfItem.DataType.ToLower() == "datetime") { tb.TextAlignment = TextAlignment.Right; } c = tb; break; case ControlType.Combobox: ComboBox cbb = new ComboBox(); if (dfItem.ReferenceDataInfo != null) { cbb.ItemsSource = this.GetItemSource(dfItem.ReferenceDataInfo.Type); cbb.DisplayMemberPath = "Text"; binding.Path = new PropertyPath(dfItem.ReferenceDataInfo.ReferencedMember); } cbb.SetBinding(ComboBox.SelectedItemProperty, binding); cbb.IsEnabled = !(IsReadOnly || dfItem.IsReadOnly); c = cbb; break; case ControlType.LookUp: FBLookUp lookUp = new FBLookUp(); lookUp.OperationType = this.OperationType; lookUp.DisplayMemberPath = dfItem.PropertyName; if (dfItem.ReferenceDataInfo != null) { lookUp.DisplayMemberPath = dfItem.ReferenceDataInfo.ReferencedMember + ".Text"; if (!string.IsNullOrEmpty(dfItem.ReferenceDataInfo.TextPath)) { lookUp.DisplayMemberPath = dfItem.ReferenceDataInfo.TextPath; } lookUp.LookUpType = dfItem.ReferenceDataInfo.Type; binding.Path = new PropertyPath(dfItem.ReferenceDataInfo.ReferencedMember); } lookUp.SetBinding(LookUp.SelectItemProperty, binding); lookUp.ReferencedDataInfo = dfItem.ReferenceDataInfo; lookUp.Parameters = dfItem.ReferenceDataInfo.Parameters; lookUp.IsReadOnly = (IsReadOnly || dfItem.IsReadOnly); c = lookUp; break; case ControlType.Remark: TextBox tbRemark = new TextBox(); tbRemark.AcceptsReturn = true; tbRemark.TextWrapping = TextWrapping.Wrap; //tbRemark.Height = 66; //tbRemark.VerticalScrollBarVisibility = ScrollBarVisibility.Visible; //tbRemark.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; tbRemark.SetBinding(TextBox.TextProperty, binding); c = tbRemark; tbRemark.IsReadOnly = (IsReadOnly || dfItem.IsReadOnly); tbRemark.MaxLength = dfItem.MaxLength; break; case ControlType.DatePicker: DatePicker datePicker = new DatePicker(); datePicker.SelectedDateFormat = DatePickerFormat.Short; datePicker.SetBinding(DatePicker.SelectedDateProperty, binding); c = datePicker; datePicker.IsEnabled = !(IsReadOnly || dfItem.IsReadOnly); break; case ControlType.DateTimePicker: DateTimePicker dateTimePicker = new DateTimePicker(); dateTimePicker.SetBinding(DateTimePicker.ValueProperty, binding); c = dateTimePicker; dateTimePicker.IsEnabled = !(IsReadOnly || dfItem.IsReadOnly); break; // Add By LVCHAO 2011.01.30 14:46 case ControlType.HyperlinkButton: HyperlinkButton hb = new HyperlinkButton(); if (dfItem.ReferenceDataInfo != null) { binding.Path = new PropertyPath(dfItem.ReferenceDataInfo.ReferencedMember + ".Text"); } hb.Click += (o, e) => { var sourceOjb = hb.DataContext.GetObjValue(dfItem.ReferenceDataInfo.ReferencedMember + ".Value"); CommonFunction.ShowExtendForm(sourceOjb); }; hb.SetBinding(HyperlinkButton.ContentProperty, binding); c = hb; break; } return(c); }