/// <summary> /// 比较对象数据是否相同 /// </summary> /// <param name="instance1">对象1</param> /// <param name="instance1">对象2</param> /// <returns></returns> public static bool EqualsValue( XDependencyObject instance1, XDependencyObject instance2) { if (instance1 == instance2) { return(true); } if (instance1 == null || instance2 == null) { return(false); } if (instance1.InnerValues.Count != instance2.InnerValues.Count) { return(false); } foreach (XDependencyProperty p in instance1.InnerValues.Keys) { if (instance2.InnerValues.ContainsKey(p)) { object v = instance1.InnerValues[p]; object v2 = instance2.InnerValues[p]; if (object.Equals(v, v2) == false) { return(false); } } else { return(false); } } return(true); }
public void Log( XDependencyObject instance, XDependencyProperty property, object oldValue, object newValue) { if (this.Enabled == false) { return; } foreach (LogItem item in this._Items) { if (item.Instance == instance && item.Property == property) { item.NewValue = newValue; return; } } LogItem newItem = new LogItem(); newItem.Instance = instance; newItem.Property = property; newItem.OldValue = oldValue; newItem.NewValue = newValue; this._Items.Add(newItem); }
public static void ApplyDefaultValuePropertyNames(XDependencyObject instance, string names) { if (string.IsNullOrEmpty(names) == false && instance != null) { foreach (string name in names.Split(',')) { bool match = false; foreach (XDependencyProperty p in instance.InnerValues.Keys) { if (p.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)) { match = true; break; } }//foreach if (match == false) { XDependencyProperty p = XDependencyProperty.GetProperty(instance.GetType(), name); if (p != null) { instance.InnerValues[p] = p.DefaultValue; } } }//foreach } }
public override void SetValue(object component, object value) { XDependencyObject obj = component as XDependencyObject; if (obj != null) { obj.SetValue(this._Property, value); } }
public override void ResetValue(object component) { XDependencyObject obj = component as XDependencyObject; if (obj != null) { obj.SetValue(this._Property, this._Property.DefaultValue); } }
/// <summary> /// 初始化对象 /// </summary> /// <param name="instance">对象实例</param> /// <param name="property">属性</param> /// <param name="Value">数值</param> /// <param name="newValue">新数值</param> internal XDependencyObjectEventArgs( XDependencyObject instance, XDependencyProperty property, object Value, object newValue) { _instance = instance; _Property = property; _Value = Value; _NewValue = newValue; }
public override object GetValue(object component) { XDependencyObject obj = component as XDependencyObject; if (obj == null) { return(null); } else { return(obj.GetValue(this._Property)); } }
public override bool ShouldSerializeValue(object component) { XDependencyObject obj = component as XDependencyObject; if (obj != null) { object v = obj.GetValue(this._Property); if (this._Property.EqualsDefaultValue(v)) { return(false); } } return(true); }
/// <summary> /// 合并数据 /// </summary> /// <param name="source">数据源</param> /// <param name="destination">数据目标对象</param> /// <param name="overWrite">源数据是否覆盖目标数据</param> /// <returns>修改了目标对象的属性个数</returns> public static int MergeValues( XDependencyObject source, XDependencyObject destination, bool overWrite) { if (source == destination) { return(0); } if (source == null || destination == null) { return(0); } int result = 0; foreach (XDependencyProperty p in source.InnerValues.Keys) { if (destination.InnerValues.ContainsKey(p) == false) { object v = source.GetValue(p); if (v is ICloneable) { v = ((ICloneable)v).Clone(); } destination.SetValue(p, v); result++; } else { if (overWrite) { bool back = destination._DisableDefaultValue; destination._DisableDefaultValue = source._DisableDefaultValue; destination.SetValue(p, source.GetValue(p)); destination._DisableDefaultValue = back; result++; } //object v = source.GetValue( p ); //object v2 = destination.GetValue(p); //if (v != v2) //{ // destination.SetValue(p, v); // result++; //} } } return(result); }
///// <summary> ///// 获得采用默认值的属性名称列表 ///// </summary> ///// <returns>属性名称列表</returns> //public static string GetDefaultValuePropertyNames(XDependencyObject instance) //{ // if (instance == null) // { // return null; // } // List<string> result = null; // foreach (XDependencyProperty p in instance.InnerValues.Keys) // { // if (p.EqualsDefaultValue(instance.InnerValues[p])) // { // if (result == null) // { // result = new List<string>(); // } // result.Add(p.Name); // } // } // if (result != null) // { // result.Sort(); // StringBuilder str = new StringBuilder(); // foreach (string name in result) // { // if (str.Length > 0) // { // str.Append(","); // } // str.Append(name); // } // return str.ToString(); // } // else // { // return null; // } //} public static string GetStyleString(XDependencyObject instance) { if (instance == null) { throw new ArgumentNullException("instance"); } StringBuilder str = new StringBuilder(); foreach (XDependencyProperty p in instance._InnerValues.Keys) { if (str.Length > 0) { str.Append(";"); } str.Append(p.Name + ":" + instance._InnerValues[p]); } return(str.ToString()); }
/// <summary> /// 获得对象中所有存在的属性的名称 /// </summary> /// <param name="instance">对象</param> /// <returns>属性名称数组</returns> public static string[] GetExistPropertyNames(XDependencyObject instance) { if (instance == null) { throw new ArgumentNullException("instance"); } if (instance._InnerValues != null) { string[] result = new string[instance._InnerValues.Count]; int iCount = 0; foreach (XDependencyProperty p in instance._InnerValues.Keys) { result[iCount] = p.Name; iCount++; } return(result); } return(null); }
/// <summary> /// 获得所有的属性值组成的字典,字典键值为属性名,字典值为属性值。 /// </summary> /// <param name="instance">对象</param> /// <returns>获得的字典</returns> public static Hashtable GetPropertyValues(XDependencyObject instance) { if (instance == null) { throw new ArgumentNullException("instance"); } if (instance._InnerValues != null) { Hashtable table = new Hashtable(); foreach (XDependencyProperty p in instance._InnerValues.Keys) { table[p.Name] = instance._InnerValues[p]; } return(table); } else { return(null); } }
/// <summary> /// 删除指定名称的属性,属性名不区分大小写 /// </summary> /// <param name="instance">对象实例</param> /// <param name="propertyName">属性名</param> /// <returns>操作是否修改了数据</returns> public static bool RemoveProperty(XDependencyObject instance, string propertyName) { if (instance == null) { throw new ArgumentNullException("instance"); } if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } foreach (XDependencyProperty p in instance._InnerValues.Keys) { if (string.Compare(p.Name, propertyName, true) == 0) { instance._InnerValues.Remove(p); return(true); } } return(false); }
/// <summary> /// 快速复制对象数据,不进行默认值的判断 /// </summary> /// <param name="source">数据来源</param> /// <param name="destination">复制目标</param> public static void CopyValueFast(XDependencyObject source, XDependencyObject destination) { if (source == destination) { return; } if (source == null || destination == null) { return; } destination.InnerValues.Clear(); foreach (XDependencyProperty p in source.InnerValues.Keys) { object v = source.InnerValues[p]; if (v is ICloneable) { v = ((ICloneable)v).Clone(); } destination._InnerValues[p] = v; } }
/// <summary> /// 获得两个对象中属性值不同的属性 /// </summary> /// <param name="instance1">对象1</param> /// <param name="instance2">对象2</param> /// <returns>属性值不同的属性对象数组</returns> public static XDependencyProperty[] GetDifferenceProperties( XDependencyObject instance1, XDependencyObject instance2) { if (instance1 == null) { throw new ArgumentNullException("instance1"); } if (instance2 == null) { throw new ArgumentNullException("instance2"); } List <XDependencyProperty> result = new List <XDependencyProperty>(); foreach (XDependencyProperty p in instance1._InnerValues.Keys) { if (instance2.InnerValues.ContainsKey(p)) { object v1 = instance1.InnerValues[p]; object v2 = instance2.InnerValues[p]; if (v1 != v2) { result.Add(p); } } else { result.Add(p); } } foreach (XDependencyProperty p in instance2.InnerValues.Keys) { if (result.Contains(p) == false && instance1.InnerValues.ContainsKey(p) == false) { result.Add(p); } } return(result.ToArray()); }
/// <summary> /// 判断对象是否存在指定名称的属性值,名称不区分大小写 /// </summary> /// <param name="instance">对象实例</param> /// <param name="propertyName">属性名</param> /// <returns>是否存在指定名称的属性值</returns> public static bool HasPropertyValue(XDependencyObject instance, string propertyName) { if (instance == null) { throw new ArgumentNullException("instance"); } if (propertyName == null || propertyName.Trim().Length == 0) { throw new ArgumentNullException("propertyName"); } if (instance._InnerValues != null && instance._InnerValues.Count > 0) { propertyName = propertyName.Trim(); foreach (XDependencyProperty p in instance._InnerValues.Keys) { if (string.Compare(p.Name, propertyName, true) == 0) { return(true); } } } return(false); }