Editar

Compartilhar via


Creating a Table View

A table view displays data in one or more columns. Each row in the table represents a .NET object, and each column of the table represents a property of the object or a script value. You can define a table view that displays all the properties of an object or a subset of the properties of an object.

A Table View Display

The following example shows how Windows PowerShell displays the System.Serviceprocess.Servicecontroller object that is returned by the Get-Service cmdlet. For this object, Windows PowerShell has defined a table view that displays the Status property, the Name property (this property is an alias property for the ServiceName property), and the DisplayName property. Each row in the table represents an object returned by the cmdlet.

Status   Name               DisplayName
------   ----               -----------
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
Running  Appinfo            Application Information

Defining the Table View

The following XML shows the table view schema for displaying the System.Serviceprocess.Servicecontroller?Displayproperty=Fullname object. You must specify each property that you want displayed in the table view.

<View>
  <Name>service</Name>
  <ViewSelectedBy>
    <TypeName>System.ServiceProcess.ServiceController</TypeName>
  </ViewSelectedBy>
  <TableControl>
    <TableHeaders>
      <TableColumnHeader>
        <Width>8</Width>
      </TableColumnHeader>
      <TableColumnHeader>
        <Width>18</Width>
      </TableColumnHeader>
      <TableColumnHeader>
        <Width>38</Width>
      </TableColumnHeader>
    </TableHeaders>
    <TableRowEntries>
      <TableRowEntry>
        <TableColumnItems>
          <TableColumnItem>
           <PropertyName>Status</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>Name</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>DisplayName</PropertyName>
          </TableColumnItem>
        </TableColumnItems>
      </TableRowEntry>
    </TableRowEntries>
  </TableControl>
</View>

The following XML elements are used to define a list view:

  • The View element is the parent element of the table view. (This is the same parent element for the list, wide, and custom control views.)

  • The Name element specifies the name of the view. This element is required for all views.

  • The ViewSelectedBy element defines the objects that use the view. This element is required.

  • The GroupBy element (not shown in this example) defines when a new group of objects is displayed. A new group is started whenever the value of a specific property or script changes. This element is optional.

  • The Controls element (not shown in this example) defines the custom controls that are defined by the table view. Controls give you a way to further specify how the data is displayed. This element is optional. A view can define its own custom controls, or it can use common controls that can be used by any view in the formatting file. For more information about custom controls, see Creating Custom Controls.

  • The HideTableHeaders element (not show in this example) specifies that the table will not show any labels at the top of the table. This element is optional.

  • The TableControl element that defines the header and row information of the table. Similar to all other views, a table view can display the values of object properties or values generated by scripts.

Defining Column Headers

  1. The TableHeaders element and its child elements define what is displayed at the top of the table.

  2. The TableColumnHeader element defines what is displayed at the top of a column of the table. Specify these elements in the order that you want the headers displayed.

    There is no limit to the number of these element that you can use, but the number of TableColumnHeader elements in your table view must equal the number of TableRowEntry elements that you use.

  3. The Label element specifies the text that is displayed. This element is optional.

  4. The Width element specifies the width (in characters) of the column. This element is optional.

  5. The Alignment element specifies how the label is displayed. The label can be aligned to the left, to the right, or centered. This element is optional.

Defining the Table Rows

Table views can provide one or more definitions that specify what data is displayed in the rows of the table by using the child elements of the TableRowEntries element. Notice that you can specify multiple definitions for the rows of the table, but the headers for the rows remains the same, regardless of what row definition is used. Typically, a table will have only one definition.

In the following example, the view provides a single definition that displays the values of several properties of the System.Diagnostics.Process?Displayproperty=Fullname object. A table view can display the value of a property or the value of a script (not shown in the example) in its rows.

<TableRowEntries>
      <TableRowEntry>
        <TableColumnItems>
          <TableColumnItem>
           <PropertyName>Status</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>Name</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>DisplayName</PropertyName>
          </TableColumnItem>
        </TableColumnItems>
      </TableRowEntry>
    </TableRowEntries>

The following XML elements can be used to provide definitions for a row:

  • The TableRowEntries element and its child elements define what is displayed in the rows of the table.

  • The TableRowEntry element provides a definition of the row. At least one TableRowEntry is required; however, there is no maximum limit to the number of elements that you can add. In most cases, a view will have only one definition.

  • The EntrySelectedBy element specifies the objects that are displayed by a specific definition. This element is optional and is needed only when you define multiple TableRowEntry elements that display different objects.

  • The Wrap element specifies that text that exceeds the column width is displayed on the next line. By default, text that exceeds the column width is truncated.

  • The TableColumnItems element defines the properties or scripts whose values are displayed in the row.

  • The TableColumnItem element defines the property or script whose value is displayed in the column of the row. A TableColumnItem element is required for each column of the row. The first entry is displayed in first column, the second entry in the second column, and so on.

  • The PropertyName element specifies the property whose value is displayed in the row. You must specify either a property or a script, but you cannot specify both.

  • The ScriptBlock element specifies the script whose value is displayed in the row. You must specify either a script or a property, but you cannot specify both.

  • The FormatString element specifies a format pattern that defines how the property or script value is displayed. This element is optional.

  • The Alignment element specifies how the value of the property or script is displayed. The value can be aligned to the left, to the right, or centered. This element is optional.

Defining the Objects That Use the Table View

There are two ways to define which .NET objects use the table view. You can use the ViewSelectedBy element to define the objects that can be displayed by all the definitions of the view, or you can use the EntrySelectedBy element to define which objects are displayed by a specific definition of the view. In most cases, a view has only one definition, so objects are typically defined by the ViewSelectedBy element.

The following example shows how to define the objects that are displayed by the table view using the ViewSelectedBy and TypeName elements. There is no limit to the number of TypeName elements that you can specify, and their order is not significant.

<View>
  <Name>System.ServiceProcess.ServiceController</Name>
  <ViewSelectedBy>
    <TypeName>System.ServiceProcess.ServiceController</TypeName>
  </ViewSelectedBy>
  <TableControl>...</TableControl>
</View>

The following XML elements can be used to specify the objects that are used by the table view:

  • The ViewSelectedBy element defines which objects are displayed by the list view.

  • The TypeName element specifies the .NET object that is displayed by the view. The fully qualified .NET type name is required. You must specify at least one type or selection set for the view, but there is no maximum number of elements that can be specified.

The following example uses the ViewSelectedBy and SelectionSetName elements. Use selection sets where you have a related set of objects that are displayed using multiple views, such as when you define a list view and a table view for the same objects. For more information about how to create a selection set, see Defining Selection Sets.

<View>
  <Name>System.ServiceProcess.ServiceController</Name>
  <ViewSelectedBy>
    <SelectionSetName>.NET Type Set</SelectionSetName>
  </ViewSelectedBy>
  <TableControl>...</TableControl>
</View>

The following XML elements can be used to specify the objects that are used by the list view:

  • The ViewSelectedBy element defines which objects are displayed by the list view.

  • The SelectionSetName element specifies a set of objects that can be displayed by the view. You must specify at least one selection set or type for the view, but there is no maximum number of elements that can be specified.

The following example shows how to define the objects displayed by a specific definition of the table view using the EntrySelectedBy element. Using this element, you can specify the .NET type name of the object, a selection set of objects, or a selection condition that specifies when the definition is used. For more information about how to create a selection conditions, see Defining Conditions for Displaying Data.

Note

When creating multiple definitions of the table view you cannot specify different column headers. You can specify only what is displayed in the rows of the table, such as what objects are displayed.

<TableRowEntry>
  <EntrySelectedBy>
    <TypeName>.NET Type</TypeName>
  </EntrySelectedBy>
</TableRowEntry>

The following XML elements can be used to specify the objects that are used by a specific definition of the list view:

  • The EntrySelectedBy element defines which objects are displayed by the definition.

  • The TypeName element specifies the .NET object that is displayed by the definition. When using this element, the fully qualified .NET type name is required. You must specify at least one type, selection set, or selection condition for the definition, but there is no maximum number of elements that can be specified.

  • The SelectionSetName element (not shown) specifies a set of objects that can be displayed by this definition. You must specify at least one type, selection set, or selection condition for the definition, but there is no maximum number of elements that can be specified.

  • The SelectionCondition element (not shown) specifies a condition that must exist for this definition to be used. You must specify at least one type, selection set, or selection condition for the definition, but there is no maximum number of elements that can be specified. For more information about defining selection conditions, see Defining Conditions for Displaying Data.

Using Format Strings

Formatting strings can be added to a view to further define how the data is displayed. The following example shows how to define a formatting string for the value of the StartTime property.

<TableColumnItem>
  <PropertyName>StartTime</PropertyName>
  <FormatString>{0:MMM} {0:DD} {0:HH}:{0:MM}</FormatString>
</TableColumnItem>

The following XML elements can be used to specify a format pattern:

  • The TableColumnItem element defines the property or script whose value is displayed in the column of the row. A TableColumnItem element is required for each column of the row. The first entry is displayed in first column, the second entry in the second column, and so on.

  • The PropertyName element specifies the property whose value is displayed in the row. You must specify either a property or a script, but you cannot specify both.

  • The FormatString element specifies a format pattern that defines how the property or script value is displayed.

In the following example, the ToString method is called to format the value of the script. Scripts can call any method of an object. Therefore, if an object has a method, such as ToString, that has formatting parameters, the script can call that method to format the output value of the script.

<ListItem>
  <ScriptBlock>
    [String]::Format("{0,-10} {1,-8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))
  </ScriptBlock>
</ListItem>

The following XML element can be used to calling the ToString method:

  • The TableColumnItem element defines the property or script whose value is displayed in the column of the row. A TableColumnItem element is required for each column of the row. The first entry is displayed in first column, the second entry in the second column, and so on.

  • The ScriptBlock element specifies the script whose value is displayed in the row. You must specify either a script or a property, but you cannot specify both.

See Also

Writing a PowerShell Formatting File