Xceed DataGrid for Silverlight Documentation
Templating

Each element that is part of a grid, including the grid itself, can be templated to change its layout and behavior. The default element templates and associated styles, which provide the same appearance as the built-in Silverlight theme, are defined in the generic.xaml file and can be copied to create custom templates.

Each column's data type determines which control will be used, by default, to display its cells' content (see Table 1).

Table 1: Default "viewers"

Default Viewers Data Types
DataGridCheckBox Boolean
Nullable<Boolean>
TextBlock String, DateTime, Int16, Int32, Int64, UInt32, UInt64, Single, Double, Decimal, Enum (only if the enumeration does NOT have the FlagAttribute)
Nullable<DateTime>, Nullable<Int16>, Nullable<Int32>, Nullable<Int64>, Nullable<UInt32>, Nullable<UInt64>, Nullable<Single>, Nullable<Double>, Nullable<Decimal>
ContentControl All other data types (will be set to readonly)

Cell-Content Templates

If a "viewer" other than one of the defaults is required, a DataTemplate that contains the desired control can be used. In this case, the grid will automatically bind to the control using a OneWay binding. The DataContext of the element used as a "viewer" is the data object (e.g., Person, Order, Customer); therefore, the "viewer" can be bound to a child property of the object, if required.

Viewer Property
TextBox/TextBlock Text
ToggleButton IsChecked
Selected SelectedValue
DatePicker SelectedDate
ContentControl Content
NumericTextBox Value

<sldg:Column FieldName="Freight">
    <sldg:Column.CellContentTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Freight, StringFormat=c2}"
                       HorizontalAlignment="Center"/>
        </DataTemplate>
    </sldg:Column.CellContentTemplate>
</sldg:Column>

Cell-Editor Templates

In addition to the default cell editors, it is also possible to define custom cell editors by specifying, as a DataTemplate, the editor (i.e., FrameworkElement) to be used through the column's CellEditorTemplate property (see also the CellContentTemplate property for information on how to provide the same control to view the content rather than edit it).

When defining a custom cell editor, the first FrameworkElement in the DataTemplate will be used as the editor and the grid will automatically bind to the appropriate property, depending on the element being used (see table below). If a custom editor that does not derive from one of the types defined in the table below is required, a ContentControl whose ContentTemplate is defined can be used (see RatingCellEditor class). In this case, the grid will automatically bind to the ContentControl's Content property using a TwoWay binding. The DataContext of the element used as an editor is the data object (e.g., Person, Order, Customer); therefore, the editor can be bound to a child property of the object, if required.

Editor Property
DataGridNumericTextBox Value
DataGridTextBox/TextBox Text
DataGridCheckBox/ToggleButton (CheckBox, RadioButton) IsChecked
DataGridComboBox/Selector (ComboBox, ListBox) SelectedValue
DatePicker SelectedDate
ContentControl Content

The code for the RatingCellEditor class can be found in the Additional Resources section of the documentation.

 

Send Feedback