wpf-devexpress-dot-xpf-dot-grid-dot-gridcontrol-e84af385.md
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
public ICommand<ColumnDisplayTextArgs> CustomColumnDisplayTextCommand { get; set; }
Public Property CustomColumnDisplayTextCommand As ICommand(Of ColumnDisplayTextArgs)
| Type | Description |
|---|---|
| ICommand<ColumnDisplayTextArgs> |
Contains properties that identify the processed cell.
|
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.
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
<dxg:GridControl ItemsSource="{Binding InvoiceList}"
AutoGenerateColumns="AddNew"
CustomColumnDisplayTextCommand="{Binding CustomColumnDisplayTextCommand}"/>
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)";
}
}
}
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
AutoGenerateColumns="AddNew"
CustomColumnDisplayTextCommand="{Binding CustomColumnDisplayTextCommand}"/>
</Grid>
See Also