Beispiel #1
0
        public override string GetFormatName(GraphProperty property)
        {
            DisplayFormatAttribute formatAttribute = property.GetAttributes <DisplayFormatAttribute>().FirstOrDefault();

            return(formatAttribute != null && !string.IsNullOrWhiteSpace(formatAttribute.DataFormatString) ?
                   formatAttribute.DataFormatString : null);
        }
        public FieldInfoAttribute(FieldType fieldType)
        {
            FieldType = fieldType;

            // Set some LabelFormat for a few specific data types
            switch (fieldType)
            {
            case FieldType.Date:
                DisplayFormat = new DisplayFormatAttribute();
                DisplayFormat.DataFormatString      = "{0:d}";
                DisplayFormat.ApplyFormatInEditMode = true;
                break;

            case FieldType.Time:
                DisplayFormat = new DisplayFormatAttribute();
                DisplayFormat.DataFormatString      = "{0:t}";
                DisplayFormat.ApplyFormatInEditMode = true;
                break;

            case FieldType.Currency:
                DisplayFormat = new DisplayFormatAttribute();
                DisplayFormat.DataFormatString = "{0:C}";

                // Don't set ApplyFormatInEditMode for currencies because the currency
                // symbol can't be parsed
                break;
            }
        }
 public DataTypeWithCustomDisplayFormat() : base("Custom datatype")
 {
     DisplayFormat = new DisplayFormatAttribute
     {
         DataFormatString = "value",
     };
 }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string Format <T>(this int input, string property)
        {
            PropertyInfo           prop = typeof(T).GetProperty(property, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            DisplayFormatAttribute displayFormatAttribute = (DisplayFormatAttribute)prop.GetCustomAttributes(typeof(DisplayFormatAttribute), true).FirstOrDefault();

            return(string.Format(displayFormatAttribute.DataFormatString, input));
        }
        public void NullDisplayTextResourceType_Get_Set()
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            attribute.NullDisplayTextResourceType = typeof(FakeResourceType);

            Assert.Equal(typeof(FakeResourceType), attribute.NullDisplayTextResourceType);
        }
Beispiel #6
0
 private static object FormatForDisplay(DisplayFormatAttribute displayFormat, object value)
 {
     if (!string.IsNullOrEmpty(displayFormat.DataFormatString))
     {
         return(string.Format(displayFormat.DataFormatString, value));
     }
     return(value);
 }
Beispiel #7
0
 public DerivedDataTypeAttribute() : base(DataType.Time)
 {
     DisplayFormat = new DisplayFormatAttribute
     {
         ApplyFormatInEditMode = true,
         DataFormatString      = DerivedDataTypeAttributeFormatString,
     };
 }
 public DataTypeWithCustomEditFormat() : base("Custom datatype")
 {
     DisplayFormat = new DisplayFormatAttribute
     {
         ApplyFormatInEditMode = true,
         DataFormatString      = "value",
     };
 }
        public void ConvertEmptyStringToNull_Set_GetReturnsExpected()
        {
            var attribute = new DisplayFormatAttribute {
                ConvertEmptyStringToNull = false
            };

            Assert.False(attribute.ConvertEmptyStringToNull);
        }
        public void GetNullDisplayText_WhenNullDisplayTextNotSet(Type input)
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            attribute.NullDisplayTextResourceType = input;

            Assert.Null(attribute.GetNullDisplayText());
        }
        public void GetNullDisplayText_NullDisplayText_ReturnsNull(Type input)
        {
            var attribute = new DisplayFormatAttribute {
                NullDisplayTextResourceType = input
            };

            Assert.Null(attribute.GetNullDisplayText());
        }
        public void ApplyFormatInEditMode_Set_GetReturnsExpected()
        {
            var attribute = new DisplayFormatAttribute {
                ApplyFormatInEditMode = true
            };

            Assert.True(attribute.ApplyFormatInEditMode);
        }
        public void HtmlEncode_Get_Set()
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            attribute.HtmlEncode = false;

            Assert.False(attribute.HtmlEncode);
        }
        public void HtmlEncode_Set_GetReturnsExpected()
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute {
                HtmlEncode = false
            };

            Assert.False(attribute.HtmlEncode);
        }
        public void ApplyFormatInEditMode_Get_Set()
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            attribute.ApplyFormatInEditMode = true;

            Assert.True(attribute.ApplyFormatInEditMode);
        }
        public void ConvertEmptyStringToNull_Get_Set()
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            attribute.ConvertEmptyStringToNull = false;

            Assert.False(attribute.ConvertEmptyStringToNull);
        }
        public void DataFormatString_Get_Set(string input)
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            attribute.DataFormatString = input;

            Assert.Equal(input, attribute.DataFormatString);
        }
        public void DataFormatString_Set_GetReturnsExpected(string input)
        {
            var attribute = new DisplayFormatAttribute {
                DataFormatString = input
            };

            Assert.Equal(input, attribute.DataFormatString);
        }
        public void NullDisplayTextResourceType_Set_GetReturnsExpected()
        {
            var attribute = new DisplayFormatAttribute {
                NullDisplayTextResourceType = typeof(FakeResourceType)
            };

            Assert.Equal(typeof(FakeResourceType), attribute.NullDisplayTextResourceType);
        }
 public FormatAttribute(DisplayFormatAttribute orig)
     : base()
 {
     this.HtmlEncode = orig.HtmlEncode;
     this.ConvertEmptyStringToNull = orig.ConvertEmptyStringToNull;
     this.DataFormatString         = orig.DataFormatString;
     this.ApplyFormatInEditMode    = orig.ApplyFormatInEditMode;
     this.NullDisplayText          = orig.NullDisplayText;
 }
        public void GetNullDisplayText_InvalidResourceType_ThrowsInvalidOperationException(Type nullDisplayTextResourceType, string nullDisplayText)
        {
            var attribute = new DisplayFormatAttribute {
                NullDisplayTextResourceType = nullDisplayTextResourceType
            };

            attribute.NullDisplayText = nullDisplayText;
            Assert.Throws <InvalidOperationException>(() => attribute.GetNullDisplayText());
        }
        public void NullDisplayText_NotAResourceType()
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            // Setting a type that is not a resource type
            attribute.NullDisplayTextResourceType = typeof(string);

            attribute.NullDisplayText = "foo";
            Assert.Throws <InvalidOperationException>(() => attribute.GetNullDisplayText());
        }
        string CheckNullDisplayText()
        {
            DisplayFormatAttribute displayFormat = GetDisplayFormat();

            if (displayFormat == null)
            {
                return(String.Empty);
            }

            return(displayFormat.NullDisplayText);
        }
        DisplayFormatAttribute GetDisplayFormat()
        {
            MetaModel.GetDataFieldAttribute <DisplayFormatAttribute> (Attributes, ref displayFormatAttr);
            if (displayFormatAttr == null)
            {
                var dta = DataTypeAttribute;
                displayFormatAttr = dta == null ? null : dta.DisplayFormat;
            }

            return(displayFormatAttr);
        }
        public void Ctor_Default()
        {
            var attribute = new DisplayFormatAttribute();

            Assert.True(attribute.ConvertEmptyStringToNull);
            Assert.True(attribute.HtmlEncode);
            Assert.False(attribute.ApplyFormatInEditMode);

            Assert.Null(attribute.DataFormatString);
            Assert.Null(attribute.NullDisplayText);
        }
        public void NullDisplayText_Set_GetReturnsExpected(string input)
        {
            var attribute = new DisplayFormatAttribute {
                NullDisplayText = input
            };

            Assert.Equal(input, attribute.GetNullDisplayText());

            // Set again, to cover the setter avoiding operations if the value is the same
            attribute.NullDisplayText = input;
            Assert.Equal(input, attribute.NullDisplayText);
        }
Beispiel #27
0
        public object TextBox <TValue>(Expression <Func <TRecord, TValue> > expression, object htmlAttributes = null)
        {
            string propertyName = PropertyNameFromLambda(expression);

            if (!_propertyNames.Contains(propertyName))
            {
                _propertyNames.Add(propertyName);
            }

            TagBuilder textBox = new TagBuilder("input");

            textBox.MergeAttribute("type", "text");
            textBox.MergeAttribute("name", propertyName);
            textBox.MergeAttribute("id", ControlId(propertyName));
            if (htmlAttributes != null)
            {
                textBox.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            }
            string displayValue = string.Empty;

            if (IsSavedRow())
            {
                var    function = expression.Compile();
                object rawValue = function.Invoke(_record);

                if (rawValue != null)
                {
                    displayValue = rawValue.ToString();

                    PropertyInfo pi   = _record.GetType().GetProperty(propertyName);
                    object[]     attr = pi.GetCustomAttributes(typeof(DisplayFormatAttribute), false);
                    if (attr.Length == 1)
                    {
                        DisplayFormatAttribute format = (DisplayFormatAttribute)attr[0];
                        displayValue = string.Format(format.DataFormatString, rawValue);
                    }
                }

                textBox.MergeAttribute("value", displayValue);
                WriteSpans(propertyName, textBox, displayValue);
            }
            else
            {
                object defaultText;
                if (DefaultValueExists(propertyName, out defaultText))
                {
                    textBox.MergeAttribute("value", defaultText.ToString());
                }
                textBox.WriteTo(_writer, _encoder);
            }

            return(null);
        }
        public void GetNullDisplayText_ValidResource_ReturnsExpected()
        {
            var attribute = new DisplayFormatAttribute {
                NullDisplayTextResourceType = typeof(FakeResourceType)
            };

            attribute.NullDisplayText = "Resource1";
            Assert.Equal(FakeResourceType.Resource1, attribute.GetNullDisplayText());

            // Changing target resource
            attribute.NullDisplayText = "Resource2";
            Assert.Equal(FakeResourceType.Resource2, attribute.GetNullDisplayText());
        }
        public void DefaultAttributeValues() {
            // Setup
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            // Verify
#if !SILVERLIGHT
            Assert.IsTrue(attribute.HtmlEncode);
#endif
            Assert.IsTrue(attribute.ConvertEmptyStringToNull);
            Assert.IsFalse(attribute.ApplyFormatInEditMode);
            Assert.IsNull(attribute.DataFormatString);
            Assert.IsNull(attribute.NullDisplayText);
        }
        public void Ctor()
        {
            DisplayFormatAttribute attribute = new DisplayFormatAttribute();

            Assert.True(attribute.ConvertEmptyStringToNull);
            Assert.True(attribute.HtmlEncode);
            Assert.False(attribute.ApplyFormatInEditMode);

            Assert.Null(attribute.DataFormatString);
            Assert.Null(attribute.NullDisplayText);
#if netcoreapp
            Assert.Null(attribute.NullDisplayTextResourceType);
#endif
        }
Beispiel #31
0
        public ChoFileRecordFieldConfiguration(string name, ChoFileRecordFieldAttribute attr = null, Attribute[] otherAttrs = null) : base(name, attr, otherAttrs)
        {
            Truncate = true;
            //IgnoreFieldValueMode = ChoIgnoreFieldValueMode.Any;

            if (attr != null)
            {
                FillChar = attr.FillCharInternal;
                FieldValueJustification = attr.FieldValueJustificationInternal;
                FieldValueTrimOption    = attr.FieldValueTrimOptionInternal;
                Truncate = attr.Truncate;
                Size     = attr.SizeInternal;

                if (Size == null && otherAttrs != null)
                {
                    StringLengthAttribute slAttr = otherAttrs.OfType <StringLengthAttribute>().FirstOrDefault();
                    if (slAttr != null && slAttr.MaximumLength > 0)
                    {
                        Size = slAttr.MaximumLength;
                    }
                }
                DisplayAttribute dpAttr = otherAttrs.OfType <DisplayAttribute>().FirstOrDefault();
                if (dpAttr != null)
                {
                    if (!dpAttr.ShortName.IsNullOrWhiteSpace())
                    {
                        FieldName = dpAttr.ShortName;
                    }
                    else if (!dpAttr.Name.IsNullOrWhiteSpace())
                    {
                        FieldName = dpAttr.Name;
                    }
                }
                DisplayFormatAttribute dfAttr = otherAttrs.OfType <DisplayFormatAttribute>().FirstOrDefault();
                if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace())
                {
                    FormatText = dfAttr.DataFormatString;
                }
                if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace())
                {
                    NullValue = dfAttr.NullDisplayText;
                }
                else
                {
                    NullValue = attr.NullValue;
                }

                QuoteField = attr.QuoteFieldInternal;
            }
        }
Beispiel #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.ComponentModel.DataAnnotations.DataTypeTypeAttribute"/> class by using the specified type name.
 /// </summary>
 /// <param name="dataType">The name of the type to associate with the data field.</param>
 public DataTypeAttribute(DataType dataType)
 {
     DataType = dataType;
     switch (dataType)
     {
         case DataType.Date:
             DisplayFormat = new DisplayFormatAttribute();
             DisplayFormat.DataFormatString = "{0:d}";
             DisplayFormat.ApplyFormatInEditMode = true;
             break;
         case DataType.Time:
             DisplayFormat = new DisplayFormatAttribute();
             DisplayFormat.DataFormatString = "{0:t}";
             DisplayFormat.ApplyFormatInEditMode = true;
             break;
         case DataType.Currency:
             DisplayFormat = new DisplayFormatAttribute();
             DisplayFormat.DataFormatString = "{0:C}";
             break;
     }
 }