Condividi tramite


DisplayAttribute Class

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

Specifies localizable strings for entity types and members that are used in the user interface.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.ComponentModel.DataAnnotations.DisplayAttribute

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

Syntax

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

The DisplayAttribute type exposes the following members.

Constructors

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

Top

Properties

  Name Description
Public property AutoGenerateField Gets or sets a value that indicates whether the field is automatically displayed in the user interface.
Public property AutoGenerateFilter Gets or sets a value that indicates whether filtering is automatically displayed for this field.
Public property Description Gets or sets either a resource name or non-localized text, such as a tool tip, that is displayed to users to help them understand a member.
Public property GroupName Gets or sets either a resource name or non-localized text that is displayed to users when grouping fields in the user interface.
Public property Name Gets or sets either a resource name or non-localized text that is used as a member label for elements that are bound to the member.
Public property Order Gets or sets a value that indicates the order in which columns are sorted.
Public property Prompt Gets or sets either a resource name or non-localized text, such as a watermark, that suggests a user action for an element that is bound to the member.
Public property ResourceType Gets or sets the type that contains the resources for other localizable property values.
Public property ShortName Gets or sets either a resource name or non-localized text that is used as the column label for an element that is bound to the member.

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 GetAutoGenerateField Retrieves the value of the AutoGenerateField property.
Public method GetAutoGenerateFilter Retrieves the value of the AutoGenerateFilter property.
Public method GetDescription Retrieves the localized or non-localized value of the Description property.
Public method GetGroupName Retrieves the localized or non-localized value of the GroupName property.
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetName Retrieves the localized or non-localized value of the Name property.
Public method GetOrder Retrieves the value of the Order property.
Public method GetPrompt Retrieves the localized or non-localized value of the Prompt property.
Public method GetShortName Retrieves the localized or non-localized value of the ShortName property.
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 DisplayAttribute attribute to a property to specify values that are used to enhance the display of the property values. When a data field is bound to a property, controls, such as the DataGrid control, automatically incorporate values from the DisplayAttribute attribute. The DisplayAttribute class provides the following properties for customizing string values that are displayed for the data field:

  • Name - The full name to use as a label for the field, such as "Social Security Number".

  • ShortName - A compressed or abbreviated version of the name to use as a label for the field, such as "SSN". This value is used by the DataGrid control.

  • Description - The text that appears in the user interface to provide more information about the field. The text can be used as a tool tip.

  • Prompt - The text that is intended as a water mark for the field to suggest a user action.

  • GroupName - A name that is used when grouping data.

  • Order - A number that indicates the position of a user-interface element that represents the field, relative to other fields. This value is used by the DataGrid control to establish the column ordering.

  • AutoGenerateField - A value that indicates whether the field is included in the automatic generation of user-interface elements such as columns. This value is used by the DataGrid control.

  • AutoGenerateFilter - A value that indicates whether a user-interface element that is automatically generated for the field should include filtering capabilities.

Examples

The following example shows how to use the DisplayAttribute attribute to set both localized and non-localized display values. A property named EmailAddress includes non-localized display values and a property named LastName includes localized display values that are retrieved from a resource type named DisplayResources.

Public Class Customer
  <Display(Name:="Email Address", Description:="An email address is needed to provide notifications about the order.")> _
  Public Property EmailAddress() As String
    'Implement Get and Set logic
  End Property

  <Display(ResourceType:=GetType(DisplayResources), Name:="LName", Description:="LNameDescription")> _
  Public Property LastName() As String
    'Implement Get and Set logic
  End Property
End Class
public class Customer
{
  [Display(Name = "Email Address", 
     Description = "An email address is needed to provide notifications about the order.")]
  public string EmailAddress { get; set; }

  [Display(ResourceType=typeof(DisplayResources), Name="LName", Description="LNameDescription")]
  public string LastName { 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.