Back to Devexpress

GridControl.CustomColumnDisplayTextCommand Property

wpf-devexpress-dot-xpf-dot-grid-dot-gridcontrol-e84af385.md

latest5.1 KB
Original Source

GridControl.CustomColumnDisplayTextCommand Property

Gets or sets a command that customizes a data cell‘s display text.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public ICommand<ColumnDisplayTextArgs> CustomColumnDisplayTextCommand { get; set; }
vb
Public Property CustomColumnDisplayTextCommand As ICommand(Of ColumnDisplayTextArgs)

Property Value

TypeDescription
ICommand<ColumnDisplayTextArgs>

Contains properties that identify the processed cell.

|

Remarks

Bind a command to the CustomColumnDisplayTextCommand property to maintain a clean MVVM pattern. The command works like a CustomColumnDisplayText event handler and allows you to customize a data cell’s display text in a View Model.

The command is called for bound and unbound columns. The printed GridControl also displays customized text.

The DisplayTextArgs.DisplayText property contains a cell’s display text. To customize the display text, assign a string value to this property.

If column data is sorted or grouped, the SourceIndex property returns an invalid value. As a result, you cannot determine the processed row and obtain other cell values. To customize the display text, use techniques described in the following topic instead: Format Cell Values.

Example

The following example shows how to display custom text in data cells. In this example, the GridControl adds the (SALE) string to the Product Name if a value in the Discount column is greater than 20:

View Example: How to display custom text within data cells

xaml
<dxg:GridControl ItemsSource="{Binding InvoiceList}" 
                 AutoGenerateColumns="AddNew" 
                 CustomColumnDisplayTextCommand="{Binding CustomColumnDisplayTextCommand}"/>
cs
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
// ...
public class MainViewModel : ViewModelBase {
// ...
    [Command]
    public void CustomColumnDisplayText(ColumnDisplayTextArgs args) {
        if(args.FieldName != nameof(Invoice.ProductName) || args.SourceIndex < 0) {
            return;
        }
        if(InvoiceList[args.SourceIndex].Discount > 20) {
            args.DisplayText += " (SALE)";
        }
    }
}
vb
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm.Xpf
' ...
Public Class MainViewModel
    Inherits ViewModelBase
' ...
    <Command>
    Public Sub CustomColumnDisplayText(ByVal args As ColumnDisplayTextArgs)
        If Not Equals(args.FieldName, NameOf(Invoice.ProductName)) OrElse args.SourceIndex < 0 Then
            Return
        End If

        If InvoiceList(args.SourceIndex).Discount > 20 Then
            args.DisplayText += " (SALE)"
        End If
    End Sub
End Class

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomColumnDisplayTextCommand property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-grid-display-custom-text-in-cells/CS/DisplayCustomText_MVVM/MainWindow.xaml#L16

xml
AutoGenerateColumns="AddNew"
                     CustomColumnDisplayTextCommand="{Binding CustomColumnDisplayTextCommand}"/>
</Grid>

See Also

Format Cell Values

GridControl Class

GridControl Members

DevExpress.Xpf.Grid Namespace