DisplayFormatAttribute Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Specifies how data fields are displayed and formatted.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.ComponentModel.DataAnnotations.DisplayFormatAttribute

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field, AllowMultiple := False)> _
Public Class DisplayFormatAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
public class DisplayFormatAttribute : Attribute

The DisplayFormatAttribute type exposes the following members.

Constructors

  Name Description
Public method DisplayFormatAttribute Initializes a new instance of the DisplayFormatAttribute class.

Top

Properties

  Name Description
Public property ApplyFormatInEditMode Gets or sets a value that indicates whether the formatting string that is specified in the DataFormatString property is applied to the member when in edit mode.
Public property ConvertEmptyStringToNull Gets or sets a value that indicates whether empty string values ("") are automatically converted to nulla null reference (Nothing in Visual Basic).
Public property DataFormatString Gets or sets the string value that specifies how to display values for the member.
Public property NullDisplayText Gets or sets the text to display for a member when the value of the member is nulla null reference (Nothing in Visual Basic).

Top

Methods

  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

You apply the DisplayFormatAttribute attribute to a property to specify how those property values are rendered in the user interface. You set the DataFormatString property to provide a formatting string that is appropriate for the type of data in the property. For example, if the property contains values that you want to display as scientific notation, you can set the DataFormatString property to {0:E}.

You use the ConvertEmptyStringToNull and NullDisplayText properties to specify how nulla null reference (Nothing in Visual Basic) and empty string ("") are rendered. You use the ApplyFormatInEditMode property to specify whether the values in the attribute are applied when the user is editing the data.

Examples

The following example shows how to apply the DisplayFormatAttribute attribute to a property named ListPrice that displays currency values and a property named Size that contains nulla null reference (Nothing in Visual Basic) or empty string values.

Public Class Product
  <DisplayFormat(DataFormatString:="{0:C}")> _
  Public Property ListPrice As Decimal
    'Implement Get and Set logic
  End Property

  <DisplayFormat(ConvertEmptyStringToNull:=True, NullDisplayText:="Not specified.")> _
  Public Property Size As String
    'Implement Get and Set logic
  End Property
End Class
public class Product
{
  [DisplayFormat(DataFormatString = "{0:C}")]
  public Decimal ListPrice { get; set; }

  [DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "Not specified.")]
  public string Size { get; set; }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.