Back to Devexpress

TreeListView.CellValueChangingCommand Property

wpf-devexpress-dot-xpf-dot-grid-dot-treelistview-3673dcce.md

latest5.5 KB
Original Source

TreeListView.CellValueChangingCommand Property

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

Declaration

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

Property Value

TypeDescription
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).

|

Remarks

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.

Edit Form

If you process data edit operations in the Edit Form, you need to cast CellValueChangedArgs to CellValueChangedInEditFormArgs.

csharp
[Command]
public void SynchronizeValues(CellValueChangedArgs args) {
    var editFormArgs = (CellValueChangedInEditFormArgs)args;
    // ...
}
vb
<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 node, 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.

csharp
[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;
    }
}
vb
<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

See Also

CellValueChanging

CellValueChangedCommand

TreeListView Class

TreeListView Members

DevExpress.Xpf.Grid Namespace