Skip to content

Commit 2907a03

Browse files
Merge pull request #4 from sergepilipchuk/sp-update-readme-24-2-1
Update Readme file (New template)
2 parents 29527dc + 57634ee commit 2907a03

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

Images/custom-text-in-cells.jpg

55 KB
Loading

Readme.md

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,76 @@
55
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
66
<!-- default badges end -->
77

8-
# WPF Data Grid - Display custom text in cells based on a specific condition
8+
# WPF Data Grid Display Custom Text in Cells Based on a Condition
99

10-
This example shows how to display custom text in data cells. In this example, the [GridControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl) adds the `(SALE)` string to the **Product Name** if a value in the **Discount** column is greater than 20.
10+
In this example, the [`GridControl`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl) changes cell display text when a specific condition is met.
1111

12-
![](https://docs.devexpress.com/WPF/images/GridControl_CustomColumnDisplayText.png?v=21.2)
12+
If the **Discount** value is greater than 20, the grid appends **(SALE)** to the **Product Name**.
1313

14-
<!-- default file list -->
14+
![Custom Text in Cells Based on a Condition](./Images/custom-text-in-cells.jpg)
1515

16-
## Files to Look At
16+
Use this technique when you need to:
1717

18-
### Code Behind Technique
18+
- Add labels or status markers to cell values.
19+
- Update cell text dynamically while the original data remains unchanged.
20+
- Improve readability and highlight key values.
1921

20-
- [MainWindow.xaml](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml) ([MainWindow.xaml](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml))
21-
- [MainWindow.xaml.cs](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml.cs#L20-L25) ([MainWindow.xaml.vb](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml.vb#L22-L29))
22+
## Implementation Details
2223

23-
### MVVM Technique
24+
The example includes [code-behind](#code-behind) and [MVVM](#mvvm) techniques.
2425

25-
- [MainWindow.xaml](./CS/DisplayCustomText_MVVM/MainWindow.xaml) ([MainWindow.xaml](./VB/DisplayCustomText_MVVM/MainWindow.xaml))
26-
- [MainViewModel.cs](./CS/DisplayCustomText_MVVM/MainViewModel.cs#L32-L40) ([MainViewModel.vb](./VB/DisplayCustomText_MVVM/MainViewModel.vb#L76-L84))
26+
### Code-Behind
2727

28-
<!-- default file list end -->
28+
Handle the [`CustomColumnDisplayText`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayText) event to replace or extend the text displayed in a cell based on custom conditions.
29+
30+
```csharp
31+
void grid_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) {
32+
if (e.Column.FieldName == "ProductName") {
33+
decimal discount = (decimal)e.GetCellValue("Discount");
34+
if (discount > 20)
35+
e.DisplayText += " (SALE)";
36+
}
37+
}
38+
```
39+
40+
### MVVM
41+
42+
Bind the [CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayTextCommand) to a view model command to change the text displayed in a cell based on custom conditions.
43+
44+
```csharp
45+
public void OnCustomColumnDisplayText(CustomColumnDisplayTextEventArgs e) {
46+
if (e.Column.FieldName == "ProductName") {
47+
decimal discount = (decimal)e.GetCellValue("Discount");
48+
if (discount > 20)
49+
e.DisplayText += " (SALE)";
50+
}
51+
}
52+
```
53+
54+
## Files to Review
55+
56+
### Code-Behind
57+
58+
* [MainWindow.xaml](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml) (VB: [MainWindow.xaml](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml))
59+
* [MainWindow.xaml.cs](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml.cs#L20-L25) (VB: [MainWindow.xaml.vb](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml.vb#L22-L29))
60+
61+
### MVVM
62+
63+
* [MainWindow.xaml](./CS/DisplayCustomText_MVVM/MainWindow.xaml) (VB: [MainWindow.xaml](./VB/DisplayCustomText_MVVM/MainWindow.xaml))
64+
* [MainViewModel.cs](./CS/DisplayCustomText_MVVM/MainViewModel.cs#L32-L40) (VB: [MainViewModel.vb](./VB/DisplayCustomText_MVVM/MainViewModel.vb#L76-L84))
2965

3066
## Documentation
3167

32-
- [Format Cell Values](https://docs.devexpress.com/WPF/400449/controls-and-libraries/data-grid/appearance-customization/format-cell-values)
33-
- [GridControl.CustomColumnDisplayText](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayText)
34-
- [TreeListView.CustomColumnDisplayText](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.TreeListView.CustomColumnDisplayText)
35-
- [GridControl.CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayTextCommand)
36-
- [TreeListView.CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.TreeListView.CustomColumnDisplayTextCommand)
68+
* [GridControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl)
69+
* [Format Cell Values](https://docs.devexpress.com/WPF/400449/controls-and-libraries/data-grid/appearance-customization/format-cell-values)
70+
* [GridControl.CustomColumnDisplayText](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayText)
71+
* [GridControl.CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayTextCommand)
3772

3873
## More Examples
3974

40-
- [How to Display custom text within data cells and groups](https://github.com/DevExpress-Examples/how-to-display-custom-text-within-data-cells-and-groups-t327301)
41-
- [How to Apply Custom Rules to Group Rows](https://github.com/DevExpress-Examples/how-to-implement-custom-grouping-e1530)
75+
* [Display Custom Text Within Data Cells and Groups](https://github.com/DevExpress-Examples/how-to-display-custom-text-within-data-cells-and-groups-t327301)
76+
* [Apply Custom Rules to Group Rows](https://github.com/DevExpress-Examples/how-to-implement-custom-grouping-e1530)
77+
4278
<!-- feedback -->
4379
## Does this example address your development requirements/objectives?
4480

0 commit comments

Comments
 (0)