wpf-devexpress-dot-xpf-dot-grid-dot-gridviewbase-ee4f6ee7.md
Gets or sets a command executed when a user edits a cell value (for example, types or deletes a character, chooses a value from the drop-down list).
Namespace : DevExpress.Xpf.Grid
Assembly : DevExpress.Xpf.Grid.v25.2.dll
NuGet Package : DevExpress.Wpf.Grid.Core
public ICommand<CellValueChangedArgs> CellValueChangingCommand { get; set; }
Public Property CellValueChangingCommand As ICommand(Of CellValueChangedArgs)
| Type | Description |
|---|---|
| ICommand<CellValueChangedArgs> |
A command executed when a user edits a cell value (for example, types or deletes a character, chooses a value from the drop-down list).
|
Bind a command to the CellValueChangingCommand property to maintain a clean MVVM pattern. The command works like a CellValueChanging event handler and allows you to process cell value edits in a View Model.
The GridControl does not call a command bound to CellValueChangingCommand when you change a cell value in code.
Refer to the following topic for more information: Obtain and Set Cell Values.
If you process data edit operations in the Edit Form, you need to cast CellValueChangedArgs to CellValueChangedInEditFormArgs.
[Command]
public void SynchronizeValues(CellValueChangedArgs args) {
var editFormArgs = (CellValueChangedInEditFormArgs)args;
// ...
}
<Command>
Public Sub SynchronizeValues(ByVal args As CellValueChangedArgs)
Dim editFormArgs = CType(args, CellValueChangedInEditFormArgs)
' ...
End Sub
The CellValueChangedInEditFormArgs class contains the CellEditors property that returns an array of CellEditorData objects. Each object allows you to specify editor’s settings. When users edit a row, you may want to initialize values or make certain editors read-only. To do that, specify the corresponding CellEditorData.Value property or set the CellEditorData.ReadOnly property to true.
The following code sample disables the Price editor depending on the CanEdit value and assigns the result of Price and Amount multiplication to the PositionValue editor.
[Command]
public void SynchronizeValues(CellValueChangedArgs args) {
// ...
if(args.FieldName == nameof(DataItem.CanEdit)) {
var priceData = editFormArgs.CellEditors.FirstOrDefault(x => x.FieldName == nameof(DataItem.Price));
priceData.ReadOnly = !bool.Parse(args.Value.ToString());
return;
}
if(args.FieldName == nameof(DataItem.Price)) {
var positionValueData = editFormArgs.CellEditors.First(d => d.FieldName == nameof(DataItem.PositionValue));
var amountData = editFormArgs.CellEditors.First(d => d.FieldName == nameof(DataItem.Amount));
int price = 0;
int.TryParse((string)args.Value, out price);
positionValueData.Value = (int)amountData.Value * price;
}
}
<Command>
Public Sub SynchronizeValues(ByVal args As CellValueChangedArgs)
' ...
If Equals(args.FieldName, NameOf(DataItem.CanEdit)) Then
Dim priceData = editFormArgs.CellEditors.FirstOrDefault(Function(x) Equals(x.FieldName, NameOf(DataItem.Price)))
priceData.[ReadOnly] = Not Boolean.Parse(args.Value.ToString())
Return
End If
If Equals(args.FieldName, NameOf(DataItem.Price)) Then
Dim positionValueData = editFormArgs.CellEditors.First(Function(d) Equals(d.FieldName, NameOf(DataItem.PositionValue)))
Dim amountData = editFormArgs.CellEditors.First(Function(d) Equals(d.FieldName, NameOf(DataItem.Amount)))
Dim price As Integer = 0
Call Integer.TryParse(CStr(args.Value), price)
positionValueData.Value = CInt(amountData.Value) * price
End If
End Sub
View Example: Data Grid for WPF - How to Process Related Cells in the Edit Form
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CellValueChangingCommand 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-data-grid-edit-form-related-cells/CS/SynchronizeEditValuesInEditForm_MVVM/MainWindow.xaml#L22
<dxg:GridControl.View>
<dxg:TableView EditFormShowMode="Inline" CellValueChangingCommand="{Binding SynchronizeValuesCommand}" RowEditStartingCommand="{Binding InitializeEditingCommand}" />
</dxg:GridControl.View>
See Also