/// <summary> /// Converts the specified value to the given string format as per the property conversion rules. /// Multiple values are each converted to the edit string format and combined into a delimited string. /// </summary> /// <param name="value">The value to convert.</param> /// <param name="format">The string format to use.</param> /// <returns>A string that represents the given value(s) in the given format.</returns> public string ValueToString(object value, ValueFormat format) { object cVal = ResolveValue(value, format); IList lst = cVal as IList; return(lst != null?ListToString(lst, format) : Convert.ToString(cVal)); }
/// <summary> /// Resolves the given value or a list of values to the specified format based on the current property configuration. /// If the property is restricted or the value is null and the format is string based, /// the <c> RestrictedString</c> or <c>NullString</c> are returned respectively. /// If the property is multivalued it will try to convert the value to a list or parse it into a list if it's a string /// or just add it to a new list as is and then convert each value in the list into the given format. /// Otherwise it will try to convert the single value to the given format. /// If a custom value converter is set on the property, it will be used first before the default property conversion rules are applied. /// </summary> /// <param name="value">The value or list of values to resolve to the given format.</param> /// <param name="format">The format to convert the value to.</param> /// <returns>A value or a list of values resolved to the given format based on the property configuration.</returns> public object ResolveValue(object value, ValueFormat format) { if (IsRestricted()) { return(format.IsString() ? RestrictedString : value); } if (IsValueNull(value, format)) { return(format.IsString() ? NullString : null); } if (IsMultiValued) { IList lst = new List <object>(); if (value is IList) { lst = (IList)value; } else if (value is string) { string[] vals = ((string)value).Split( ParseListSeparators, StringSplitOptions.None); foreach (string val in vals) { if (!IsValueNull(val, format)) { lst.Add(val); } } } else { lst.Add(value); } IList resLst = CreateList(format); foreach (object val in lst) { object cval = val; if (ValueConverter == null || !ValueConverter(ref cval, format)) { cval = ConvertValue(cval, format); } if (!IsValueNull(cval, format)) { resLst.Add(cval); } } return(resLst); } else { object cval = value; if (ValueConverter == null || !ValueConverter(ref cval, format)) { cval = ConvertValue(cval, format); } return(cval); } }
/// <summary> /// Creates a new list for the given format. /// Overrides the default behavior to return a typed generic list for the Transport format. /// </summary> /// <param name="format">The format to create a new list for.</param> /// <returns>A new list for the given format.</returns> protected override IList CreateList(ValueFormat format) { // we don't have to store values in a typed list if the format is internal, // since some values may be still invalid. However, the values should be validated // before they are transfered over the network, so the transport format requires a typed list. return(format == ValueFormat.Transport ? new List <T>() : base.CreateList(format)); }
/// <summary> /// Converts a list of values to a string for the given format. /// Default implementation uses the DisplayListSeparator to concatenate the values for any format. /// Subclasses can override this behavior to differentiate between the <c>DisplayString</c> format /// and the <c>EditString</c> format and can also provide custom delimiting, e.g. comma-separated /// and a new line between every five values to get five comma-separated values per line. /// </summary> /// <param name="list">The list of values to convert to string.</param> /// <param name="format">The string format for which the conversion is required.</param> /// <returns>The string representation of the given list.</returns> public virtual string ListToString(IList list, ValueFormat format) { string res = ""; foreach (object val in list) { res += (res == "" ? "" : displayListSeparator) + Convert.ToString(val); } return(res); }
/// <summary> /// A filtering function that determines if the value of the specified property in the given row /// matches the specified criteria value using supplied operator. /// </summary> /// <param name="property">The data property of the object to match.</param> /// <param name="row">The data row to get the value from.</param> /// <param name="oper">Comparison operator to use.</param> /// <param name="criteria">The value to compare to.</param> /// <param name="caseSensitive">True to perform case-sensitive string matching, false otherwise.</param> /// <returns>True if the property value in the given row matches the specified criteria, false otherwise.</returns> public virtual bool PropertyValueMatches(DataProperty property, DataRow row, Operator oper, object criteria, bool caseSensitive) { ValueFormat format = criteria?.GetType() == typeof(string) ? ValueFormat.DisplayString : ValueFormat.Internal; object value = property.GetValue(format, row); criteria = property.ResolveValue(criteria, format); if (format.IsString() && !caseSensitive) { value = value?.ToString()?.ToLower(); criteria = criteria?.ToString()?.ToLower(); } return(oper.Matches(value, criteria)); }
/// <summary> /// Gets a value of the specified property from the given data row. /// </summary> /// <param name="dataItem">data row to get the data from.</param> /// <param name="property">proprety name to retrieve.</param> /// <param name="format">value format to return.</param> /// <returns></returns> public static object Get(object dataItem, string property, ValueFormat format) { DataRow dr = dataItem as DataRow; if (dr == null) { return(null); } DataProperty dp = dr.List[property]; if (dp == null || dp.Column < 0 || dp.Column >= dr.Count) { return(null); } return(dp.ResolveValue(dr[dp.Column], format)); }
/// <summary> /// A function to determine if the given value is considered to be null for the given format. /// Default implementation returns true if the value is null, is an empty list, /// is a string with blank spaces only or is equal to the NullString for any format. /// Subclasses can override this function to differentiate by the value format /// or to provide different or additional rules. /// </summary> /// <param name="value">The value to check for null.</param> /// <param name="format">The value format, for which the null check is performed.</param> /// <returns>True if the value is considered to be null for the given format, otherwise false.</returns> public virtual bool IsValueNull(object value, ValueFormat format) { if (value == null) { return(true); } IList lst = value as IList; if (lst != null && lst.Count == 0) { return(true); } if (value is string) { string str = ((string)value).Trim(); return(string.IsNullOrEmpty(str) || str == NullString); } return(false); // non-null non-string }
/// <summary> /// Sets the value format dependency property on a text block. /// </summary> /// <param name="obj">The text block element to set the property on.</param> /// <param name="value">The value format to set on the text block.</param> public static void SetValueFormat(TextBlock obj, ValueFormat value) { obj.SetValue(ValueFormatProperty, value); }
/// <summary> /// Gets the value of the current property in the specified format, /// using the provided data row as the data source, if applicable. /// If no row is specified, then returns the value of the property itself. /// </summary> /// <param name="format">The format to return the value in.</param> /// <param name="row">The data row to use as a source.</param> /// <returns></returns> public object GetValue(ValueFormat format, DataRow row = null) { object val = Column < 0 ? InternalValue : row == null || row.List != parent ? null : row[Column]; return(format.IsString() ? ValueToString(val, format) : ResolveValue(val, format)); }
/// <summary> /// Converts a list of values to a string for the given format. /// Default implementation uses the DisplayListSeparator to concatenate the values for any format. /// Subclasses can override this behavior to differentiate between the <c>DisplayString</c> format /// and the <c>EditString</c> format and can also provide custom delimiting, e.g. comma-separated /// and a new line between every five values to get five comma-separated values per line. /// </summary> /// <param name="list">The list of values to convert to string.</param> /// <param name="format">The string format for which the conversion is required.</param> /// <returns>The string representation of the given list.</returns> public virtual string ListToString(IList list, ValueFormat format) { string res = ""; foreach (object val in list) res += (res == "" ? "" : displayListSeparator) + Convert.ToString(val); return res; }
/// <summary> /// A function to determine if the given value is considered to be null for the given format. /// Default implementation returns true if the value is null, is an empty list, /// is a string with blank spaces only or is equal to the NullString for any format. /// Subclasses can override this function to differentiate by the value format /// or to provide different or additional rules. /// </summary> /// <param name="value">The value to check for null.</param> /// <param name="format">The value format, for which the null check is performed.</param> /// <returns>True if the value is considered to be null for the given format, otherwise false.</returns> public virtual bool IsValueNull(object value, ValueFormat format) { if (value == null) return true; IList lst = value as IList; if (lst != null && lst.Count == 0) return true; string str = value.ToString().Trim(); return string.IsNullOrEmpty(str) || str == NullString; }
/// <summary> /// Sets the value format dependency property on a text block. /// </summary> /// <param name="obj">The text block element to set the property on.</param> /// <param name="value">The value format to set on the text block.</param> public static void SetValueFormat(TextBlock obj, ValueFormat value) { obj.SetValue(ValueFormatProperty, value); }
/// <summary> /// Gets a value of the specified property from the given data row. /// </summary> /// <param name="dataItem">data row to get the data from.</param> /// <param name="property">property name to retrieve.</param> /// <param name="format">value format to return.</param> /// <returns></returns> public static object Get(object dataItem, string property, ValueFormat format) { var dr = dataItem as DataRow; return(dr?.List[property]?.GetValue(format, dr)); }
/// <summary> /// Creates a new list for the given format. The default implementation just returns a new untyped ArrayList. /// Subclasses can override it to return typed generic lists for the Transport format. /// </summary> /// <param name="format">The format to create a new list for.</param> /// <returns>A new list for the given format.</returns> protected virtual IList CreateList(ValueFormat format) { return(new List <object>()); }
/// <summary> /// Converts a single value to a given format. The default implementation does nothing to the value, /// but subclasses can implement the property specific rules for each format. /// </summary> /// <param name="value">A single value to convert to the given format.</param> /// <param name="format">The value format to convert the value to.</param> /// <returns>The value converted to the given format.</returns> protected virtual object ConvertValue(object value, ValueFormat format) { return(value); }
/// <summary> /// Resolves the given value or a list of values to the specified format based on the current property configuration. /// If the property is restricted or the value is null and the format is string based, /// the <c> RestrictedString</c> or <c>NullString</c> are returned respectively. /// If the property is multivalued it will try to convert the value to a list or parse it into a list if it's a string /// or just add it to a new list as is and then convert each value in the list into the given format. /// Otherwise it will try to convert the single value to the given format. /// If a custom value converter is set on the property, it will be used first before the default property conversion rules are applied. /// </summary> /// <param name="value">The value or list of values to resolve to the given format.</param> /// <param name="format">The format to convert the value to.</param> /// <returns>A value or a list of values resolved to the given format based on the property configuration.</returns> public object ResolveValue(object value, ValueFormat format) { if (IsRestricted()) return format.IsString() ? RestrictedString : value; if (IsValueNull(value, format)) return format.IsString() ? NullString : null; if (IsMultiValued) { IList lst = new List<object>(); if (value is IList) lst = (IList)value; else if (value is string) { string[] vals = ((string)value).Split( ParseListSeparators, StringSplitOptions.None); foreach (string val in vals) if (!IsValueNull(val, format)) lst.Add(val); } else lst.Add(value); IList resLst = CreateList(format); foreach (object val in lst) { object cval = val; if (ValueConverter == null || !ValueConverter(ref cval, format)) cval = ConvertValue(cval, format); if (!IsValueNull(cval, format)) resLst.Add(cval); } return resLst; } else { object cval = value; if (ValueConverter == null || !ValueConverter(ref cval, format)) cval = ConvertValue(cval, format); return cval; } }
/// <summary> /// Gets a string value of the current property in the specified string format, /// using the provided data row as the data source, if applicable. /// If no row is specified, then returns the value of the property itself. /// </summary> /// <param name="format">The string format to return the value in.</param> /// <param name="row">The data row to use as a source.</param> /// <returns></returns> public string GetStringValue(ValueFormat format, DataRow row = null) => GetValue(format, row)?.ToString();
/// <summary> /// Converts the specified value to the given string format as per the property conversion rules. /// Multiple values are each converted to the edit string format and combined into a delimited string. /// </summary> /// <param name="value">The value to convert.</param> /// <param name="format">The string format to use.</param> /// <returns>A string that represents the given value(s) in the given format.</returns> public string ValueToString(object value, ValueFormat format) { object cVal = ResolveValue(value, format); IList lst = cVal as IList; return lst != null ? ListToString(lst, format) : Convert.ToString(cVal); }
/// <summary> /// Converts a single value to a given format. The default implementation does nothing to the value, /// but subclasses can implement the property specific rules for each format. /// </summary> /// <param name="value">A single value to convert to the given format.</param> /// <param name="format">The value format to convert the value to.</param> /// <returns>The value converted to the given format.</returns> protected virtual object ConvertValue(object value, ValueFormat format) { return value; }
/// <summary> /// Creates a new list for the given format. The default implementation just returns a new untyped ArrayList. /// Subclasses can override it to return typed generic lists for the Transport format. /// </summary> /// <param name="format">The format to create a new list for.</param> /// <returns>A new list for the given format.</returns> protected virtual IList CreateList(ValueFormat format) { return new List<object>(); }