Example #1
0
        /// <summary>
        /// Updates specified item PropertyName with the data from data object property specified by DataMember. 
        /// </summary>
        /// <param name="dataManager">CurrencyManager to use</param>
        /// <param name="item">Item to set PropertyName on.</param>
        /// <param name="data">Data to retrieve DataMember value from.</param>
        public void Update(ItemVisualGenerator generator, object item, object data)
        {
            string propertyName = _PropertyName;

            if (item is BaseItem && _PropertyPath.Length > 1)
            {
                // Dig into child items
                BaseItem parent = (BaseItem)item;
                for (int i = 0; i < _PropertyPath.Length - 1; i++)
                {
                    parent = parent.SubItems[_PropertyPath[i]];
                }
                item = parent;
                propertyName = _PropertyPath[_PropertyPath.Length - 1];
            }
            
            PropertyInfo targetProp = item.GetType().GetProperty(propertyName);
            if (targetProp.PropertyType == typeof(string))
                targetProp.SetValue(item, GetDataText(generator, data, _DataMember), null);
            else
                targetProp.SetValue(item, GetPropertyValue(generator, data, _DataMember), null);
        }
Example #2
0
 private void EnsureBindingGenerator()
 {
     if (_BindingGenerator == null)
     {
         _BindingGenerator = new ItemVisualGenerator(this);
         _BindingGenerator.VisualTemplate = GetItemTemplate();
     }
 }
Example #3
0
 private string GetDataText(ItemVisualGenerator generator, object data, string fieldName)
 {
     object propertyValue = GetPropertyValue(generator, data, fieldName);
     return GetDataText(generator, data, fieldName, propertyValue);
 }
Example #4
0
        private object GetPropertyValue(ItemVisualGenerator generator, object data, string fieldName)
        {
            if ((data != null) && (fieldName.Length > 0))
            {
                try
                {
                    PropertyDescriptor descriptor;
                    if (generator.DataManager != null)
                    {
                        descriptor = generator.DataManager.GetItemProperties().Find(fieldName, true);
                    }
                    else
                    {
                        descriptor = TypeDescriptor.GetProperties(data).Find(fieldName, true);
                    }
                    if (descriptor != null)
                    {
                        data = descriptor.GetValue(data);
                    }
                }
                catch
                {
                }
            }

            if (data == DBNull.Value && _NullValue != null)
                return _NullValue;

            return data;
        }
Example #5
0
        private string GetDataText(ItemVisualGenerator generator, object data, string fieldName, object propertyValue)
        {
            if (!_FormattingEnabled)
            {
                if (data == null)
                {
                    return string.Empty;
                }
                if (propertyValue == null)
                {
                    return "";
                }
                return Convert.ToString(propertyValue, CultureInfo.CurrentCulture);
            }

            DataConvertEventArgs e = new DataConvertEventArgs(propertyValue, typeof(string), data, fieldName);
            this.OnFormat(e);
            if ((e.Value != data) && (e.Value is string))
            {
                return (string)e.Value;
            }
            if (stringTypeConverter == null)
            {
                stringTypeConverter = TypeDescriptor.GetConverter(typeof(string));
            }
            try
            {
                return (string)FormatHelper.FormatObject(propertyValue, typeof(string), generator.GetFieldConverter(fieldName), stringTypeConverter, _FormatString, _FormatInfo, null, DBNull.Value);
            }
            catch (Exception ex)
            {
                if (ex is SecurityException || IsCriticalException(ex))
                {
                    throw;
                }
                return ((propertyValue != null) ? Convert.ToString(data, CultureInfo.CurrentCulture) : "");
            }
        }