private static void OnDefaultObjectChanged(object o, DependencyPropertyChangedEventArgs e) { PropertySetter ps = (PropertySetter)o; if (ps.Object != null) { //默认值改变了,但是对象还没有值,给对象设置值为默认值 if (ps.Object.GetPropertyValue(ps.PropertyName) == null) { if (ps.DefaultObject is GeneralObject) { //复制默认对象到新对象 GeneralObject go = ps.DefaultObject as GeneralObject; GeneralObject ngo = new GeneralObject(); ngo.CopyFrom(go); ps.Object.SetPropertyValue(ps.PropertyName, ngo, false, true); } else if (ps.DefaultObject is ObjectList) { //复制默认对象到新对象 ObjectList go = ps.DefaultObject as ObjectList; ObjectList ngo = new ObjectList(); ngo.CopyFrom(go); ps.Object.SetPropertyValue(ps.PropertyName, ngo, false, true); } } } }
public void NewPropertyValue(string propertyName) { //有属性默认值 var p = (from ps in PropertySetters where ps.PropertyName == propertyName select ps).FirstOrDefault(); if (p != null && p.Default != null) { //设置默认属性结果 SetPropertyValue(propertyName, p.Default, true, true); //如果默认属性是列表,调用列表的New过程 if (p.Default is BaseObjectList) { (p.Default as BaseObjectList).New(); } } //有默认对象,复制默认对象 else if (p != null && p.DefaultObject != null) { if (p.DefaultObject is GeneralObject) { //复制默认对象到新对象 GeneralObject go = p.DefaultObject as GeneralObject; GeneralObject ngo = new GeneralObject(); ngo.CopyFrom(go); p.Object.SetPropertyValue(p.PropertyName, ngo, false, true); } else if (p.DefaultObject is ObjectList) { //复制默认对象到新对象 ObjectList go = p.DefaultObject as ObjectList; ObjectList ngo = new ObjectList(); ngo.CopyFrom(go); p.Object.SetPropertyValue(p.PropertyName, ngo, false, true); } } else { //如果属性是集合 if (GetProperty(propertyName).PropertyType == typeof(BaseObjectList)) { //如果属性存在,清空集合 BaseObjectList old = (BaseObjectList)this.GetPropertyValue(propertyName); if (old != null) { old.New(); } //否则,无不置空标记,设置一个空集合 else if (!NotEmpty) { SetPropertyValue(propertyName, new ObjectList(), true); } } else { SetPropertyValue(propertyName, null, true, true); } } }
//设置列表属性,设置时,将监听其IsModified属性改变事件 private void SetCollectionProperty(string key, BaseObjectList ol) { //如果有默认对象,且要设置的列表为空,采用默认对象的复制结果 var p = (from ps in PropertySetters where ps.PropertyName == key select ps).FirstOrDefault(); if (p != null && p.DefaultObject != null && (ol == null || ol.Count == 0)) { //复制默认对象到新对象 ObjectList go = p.DefaultObject as ObjectList; ObjectList ngo = new ObjectList(); ngo.CopyFrom(go); ol = ngo; } SetPropertyValue(key, ol, true); ol.Watch("IsModified", ol_PropertyChanged); }
public void CopyFrom(GeneralObject go) { //复制实体类型 if (go.entityType != null) { EntityType = go.EntityType; } foreach (string key in go._customPropertyValues.Keys) { object value = go._customPropertyValues[key]; //是列表,进行列表复制 if (value is BaseObjectList) { //如果列表不存在,新建列表 if (!_customPropertyValues.ContainsKey(key) || _customPropertyValues[key] == null) { ObjectList ol = new ObjectList(); SetPropertyValue(key, ol, true); } ObjectList nol = (ObjectList)_customPropertyValues[key]; nol.CopyFrom(value as BaseObjectList); } //是对象,进行对象复制 else if (value is GeneralObject) { SetPropertyValue(key, value, true); ////对象不存在,新建对象 //if (!_customPropertyValues.ContainsKey(key) || _customPropertyValues[key] == null) //{ // GeneralObject obj = new GeneralObject(); // SetPropertyValue(key, obj, true); //} ////进行对象复制 //GeneralObject ngo = (GeneralObject)_customPropertyValues[key]; //ngo.CopyFrom((GeneralObject)value); } //是一般属性,调用设置属性值的过程 else { this.NewGetType(); SetPropertyValue(key, value, true); } } }