Back to Devexpress

DialogEditFormBehavior Class

wpf-devexpress-dot-xpf-dot-grid-a7b13c03.md

latest14.7 KB
Original Source

DialogEditFormBehavior Class

Allows you to enable edit operations in a Data Grid bound to a Server Mode or Instant Feedback data source.

Namespace : DevExpress.Xpf.Grid

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

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public class DialogEditFormBehavior :
    Behavior<GridControl>
vb
Public Class DialogEditFormBehavior
    Inherits Behavior(Of GridControl)

Remarks

Server Mode and Instant Feedback sources do not support edit operations out-of-the-box. Use the DialogEditFormBehavior to allow users to invoke a dialog edit form where they can modify row values:

  1. Add the DialogEditFormBehavior to Data Grid behaviors. Set its KeyProperty to a key field that you can use to find rows during edit operations.

  2. Before a user invokes the dialog edit form , the behavior raises the CreateEditItemViewModel event and executes the CreateEditItemViewModelCommand. Use them to specify a View Model for the edit operation. The View Model includes the item that you want to edit or add to the Data Grid, the data context that describes the edit operation, and the dialog edit form ‘s title.

  3. Specify the behavior’s EditTemplate property to define editors in the dialog edit form. In the template, use the data context that you defined in the previous step.

  4. Allow users to invoke the dialog edit form with a double click of a row. To do that, bind the view’s RowDoubleClickCommand to the behavior’s RowDoubleClickCommand.

  5. After a user saves changes made in the dialog edit form , the behavior raises the ValidateRow event and executes the ValidateRowCommand. Use them to validate values, check database constraints, and save changes to the database.

  6. Users can remove rows from the Data Grid. To do that, they can select rows and call the DeleteCommand command.

Asynchronous Edit Operations

The DialogEditFormBehavior allows you to process edit operations asynchronously. While such an operation is in progress, users can cancel it or they can continue to interact with other controls on your form.

Create the Edit Operation’s View Model

You can specify the edit operation’s view model in a background thread. The GridControl displays the wait indicator during the operation. Follow the steps below to implement this behavior:

  1. Create a task that specifies a view model for the edit operation.
  2. Create a command and bind it to the DialogEditFormBehavior.CreateEditItemViewModelCommand property.
  3. Assign the task to the command’s GetViewModelAsync property.
xaml
<dxg:GridControl ...>
    <dxmvvm:Interaction.Behaviors>
        <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id"
            CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}">
            <!-- ... -->
        </dxg:DialogEditFormBehavior>
    </dxmvvm:Interaction.Behaviors>
</dxg:GridControl>
csharp
[Command]
public void CreateEditEntityViewModel(CreateEditItemViewModelArgs args) {
    args.GetViewModelAsync = GetViewModel(args);
}

async Task<IEditItemViewModel> GetViewModel(CreateEditItemViewModelArgs args) {
    await Task.Delay(1000);
    var context = new IssuesContext();
    Issue item;
    if (args.IsNewItem) {
        item = new Issue() { Created = DateTime.Now };
        context.Entry(item).State = EntityState.Added;
    } else {
        item = context.Issues.Find(args.Key);
    }
    return new EditItemViewModel(
        item, 
        new EditIssueInfo(context, Users), 
        title: (args.IsNewItem ? "New " : "Edit ") + nameof(Issue)
    );
}
vb
<Command>
Public Sub CreateEditEntityViewModel(ByVal args As CreateEditItemViewModelArgs)
    args.GetViewModelAsync = GetViewModel(args)
End Sub

Private Async Function GetViewModel(ByVal args As CreateEditItemViewModelArgs) As Task(Of IEditItemViewModel)
    Await Task.Delay(1000)
    Dim context = New IssuesContext()
    Dim item As Issue

    If args.IsNewItem Then
        item = New Issue() With {
            .Created = DateTime.Now
        }
        context.Entry(item).State = EntityState.Added
    Else
        item = context.Issues.Find(args.Key)
    End If

    Return New EditItemViewModel(item, New EditIssueInfo(context, Users), title:=(If(args.IsNewItem, "New ", "Edit ")) & NameOf(Issue))
End Function

Validate Changes

You can validate changes and save them to the data source in another thread. The dialog edit form displays the wait indicator in the Save button during the operation. Follow the steps below to implement this behavior:

  1. Create a task that validates changes and saves them to the data source.
  2. Create a command and bind it to the DialogEditFormBehavior.ValidateRowCommand property.
  3. Assign the task to the command’s ValidateAsync property.
  4. Set the DialogEditFormBehavior.AllowCancelAsyncOperations property to true to allow users to cancel the asynchronous post changes operation.
xaml
<dxg:GridControl ...>
    <dxmvvm:Interaction.Behaviors>
        <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id"
            CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}" 
            ValidateRowCommand="{Binding ValidateRowCommand}"
            AllowCancelAsyncOperations="True">
            <!-- ... -->
        </dxg:DialogEditFormBehavior>
    </dxmvvm:Interaction.Behaviors>
</dxg:GridControl>
csharp
[Command]
public void ValidateRow(EditFormRowValidationArgs args) {
    args.ValidateAsync = ValidateAsync(args);
}
async Task ValidateAsync(EditFormRowValidationArgs args) {
    await Task.Delay(2000);
    var context = ((EditIssueInfo)args.EditOperationContext).DbContext;
    context.SaveChanges();
    args.CancellationToken.ThrowIfCancellationRequested();
}
vb
<Command>
Public Sub ValidateRow(ByVal args As EditFormRowValidationArgs)
    args.ValidateAsync = ValidateAsync(args)
End Sub

Private Async Function ValidateAsync(ByVal args As EditFormRowValidationArgs) As Task
    Await Task.Delay(2000)
    Dim context = (CType(args.EditOperationContext, EditIssueInfo)).DbContext
    context.SaveChanges()
    args.CancellationToken.ThrowIfCancellationRequested()
End Function

Validate the Delete Row Operation

You can delete rows from a data source in a background thread. The GridControl displays the wait indicator during the operation. Follow the steps below to implement this behavior:

  1. Create a task that deletes rows form the data source.
  2. Create a command and bind it to the DialogEditFormBehavior.ValidateRowDeletionCommand property.
  3. Assign the task to the ValidateAsync property.
xaml
<dxg:GridControl ...>
    <dxmvvm:Interaction.Behaviors>
        <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id"
            CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}" 
            ValidateRowCommand="{Binding ValidateRowCommand}"
            ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}"
            AllowCancelAsyncOperations="True">
            <!-- ... -->
        </dxg:DialogEditFormBehavior>
    </dxmvvm:Interaction.Behaviors>
</dxg:GridControl>
csharp
[Command]
public void ValidateRowDeletion(EditFormValidateRowDeletionArgs args) {
    args.ValidateAsync = ValidateDeletion(args);
}

async Task ValidateDeletion(EditFormValidateRowDeletionArgs args) {
    await Task.Delay(2000);
    var key = (int)args.Keys.Single();
    var item = new Issue() { Id = key };
    var context = new IssuesContext();
    context.Entry(item).State = EntityState.Deleted;
    context.SaveChanges();
}
vb
<Command>
Public Sub ValidateRowDeletion(ByVal args As EditFormValidateRowDeletionArgs)
    args.ValidateAsync = ValidateDeletion(args)
End Sub

Private Async Function ValidateDeletion(ByVal args As EditFormValidateRowDeletionArgs) As Task
    Await Task.Delay(2000)
    Dim key = CInt(args.Keys.Single())
    Dim item = New Issue() With {
        .Id = key
    }
    Dim context = New IssuesContext()
    context.Entry(item).State = EntityState.Deleted
    context.SaveChanges()
End Function

Examples

Instant Feedback

Server Mode

The following code snippets (auto-collected from DevExpress Examples) contain references to the DialogEditFormBehavior class.

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-implement-crud-operations/CS/CodeBehind/EntityFramework/InstantFeedbackMode/MainWindow.xaml#L46

xml
<dxmvvm:Interaction.Behaviors>
    <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id"
                                CreateEditItemViewModel="OnCreateEditEntityViewModel" ValidateRow="OnValidateRow"

wpf-data-grid-extend-crud-operations/CS/DetailCollectionEditing/MainWindow.xaml#L36

xml
<dxmvvm:Interaction.Behaviors>
    <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id" CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}" ValidateRowCommand="{Binding ValidateRowCommand}" ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}">
        <dxg:DialogEditFormBehavior.EditTemplate>

Inheritance

Object DispatcherObject DependencyObject Freezable Animatable DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase DevExpress.Mvvm.UI.Interactivity.Behavior DevExpress.Mvvm.UI.Interactivity.Behavior<GridControl> DialogEditFormBehavior

See Also

DialogEditFormBehavior Members

CRUD Operations in a Data-Bound Grid

DevExpress.Xpf.Grid Namespace