wpf-devexpress-dot-xpf-dot-grid-a7b13c03.md
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
public class DialogEditFormBehavior :
Behavior<GridControl>
Public Class DialogEditFormBehavior
Inherits Behavior(Of GridControl)
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:
Add the DialogEditFormBehavior to Data Grid behaviors. Set its KeyProperty to a key field that you can use to find rows during edit operations.
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.
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.
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.
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.
Users can remove rows from the Data Grid. To do that, they can select rows and call the DeleteCommand command.
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.
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:
<dxg:GridControl ...>
<dxmvvm:Interaction.Behaviors>
<dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id"
CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}">
<!-- ... -->
</dxg:DialogEditFormBehavior>
</dxmvvm:Interaction.Behaviors>
</dxg:GridControl>
[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)
);
}
<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
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:
true to allow users to cancel the asynchronous post changes operation.<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>
[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();
}
<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
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:
<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>
[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();
}
<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
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.
<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
<dxmvvm:Interaction.Behaviors>
<dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id" CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}" ValidateRowCommand="{Binding ValidateRowCommand}" ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}">
<dxg:DialogEditFormBehavior.EditTemplate>
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