Skip to main content
A newer version of this page is available. .
All docs
V21.2

DialogEditFormBehavior Class

Allows you to enable edit operations in the Data Grid bound to Server Mode and Instant Feedback sources.

Namespace: DevExpress.Xpf.Grid

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

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public class DialogEditFormBehavior :
    Behavior<GridControl>

Remarks

Server Mode and Instant Feedback sources do not support edit operations out-of-the-box. You need to 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.

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <dxmvvm:Interaction.Behaviors>
            <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id" />
        </dxmvvm:Interaction.Behaviors>
    </dxg:GridControl>
    
  2. Before a user invokes the dialog edit form, the behavior raises the CreateEditItemViewModel event and executes the CreateEditItemViewModelCommand. Use them the specify a View Model for the edit operation. The View Model includes an 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.

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <dxmvvm:Interaction.Behaviors>
            <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id" 
                CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}" />
        </dxmvvm:Interaction.Behaviors>
    </dxg:GridControl>
    
    [DevExpress.Mvvm.DataAnnotations.Command]
    public void CreateEditEntityViewModel(DevExpress.Mvvm.Xpf.CreateEditItemViewModelArgs args) {
        var context = new IssuesContext();
        Issue item;
        if(args.Key != null)
            item = context.Issues.Find(args.Key);
        else {
            item = new Issue() { Created = DateTime.Now };
            context.Entry(item).State = EntityState.Added;
        }
        args.ViewModel = new EditItemViewModel(item, new EditIssueInfo(context, Users));
    }
    
  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.

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <dxmvvm:Interaction.Behaviors>
            <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id">
                <dxg:DialogEditFormBehavior.EditTemplate>
                    <DataTemplate>
                        <local:IssueDetailView />
                    </DataTemplate>
                </dxg:DialogEditFormBehavior.EditTemplate>
            </dxg:DialogEditFormBehavior>
        </dxmvvm:Interaction.Behaviors>
    </dxg:GridControl>
    
    <Grid>
        <dxlc:LayoutControl x:Name="layoutControl" 
                            Orientation="Vertical" VerticalAlignment="Top" Width="300">
            <dxlc:DataLayoutItem Binding="{Binding Item.Subject}"/>
            <dxlc:LayoutItem Label="User">
                <dxe:ComboBoxEdit EditValue="{Binding Item.UserId}" ItemsSource="{Binding Tag.Users}" 
                                  ValueMember="Id" DisplayMember="Name" IsTextEditable="False"/>
            </dxlc:LayoutItem>
            <dxlc:DataLayoutItem Binding="{Binding Item.Created}"/>
            <dxlc:DataLayoutItem Binding="{Binding Item.Votes}"/>
            <dxlc:DataLayoutItem Binding="{Binding Item.Priority}"/>
        </dxlc:LayoutControl>
    </Grid> 
    
  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.

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <dxg:GridControl.View>
            <dxg:TableView NavigationStyle="Row"
                RowDoubleClickCommand="{Binding RowDoubleClickCommand, ElementName=EditFormBehavior}" />
      </dxg:GridControl.View>
        <!-- ... -->
    </dxg:GridControl>
    

    Alternatively, you can specify InputBindings.

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <!-- ... -->
        <dxg:GridControl.InputBindings>
            <KeyBinding Command="{Binding UpdateCommand, ElementName=EditFormBehavior}" Key="F2" />
            <KeyBinding Command="{Binding CreateCommand, ElementName=EditFormBehavior}" Key="N" Modifiers="Control" />
        </dxg:GridControl.InputBindings>
    </dxg:GridControl>
    

    You can also bind behavior commands to button actions.

    <dxb:ToolBarControl>
        <dxb:BarButtonItem Content="Edit (F2)" Command="{Binding UpdateCommand, ElementName=EditFormBehavior}" />
        <dxb:BarButtonItem Content="New" Command="{Binding CreateCommand, ElementName=EditFormBehavior}" />
    </dxb:ToolBarControl> 
    
  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.

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <dxmvvm:Interaction.Behaviors>
            <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id" 
                    CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}" 
                    ValidateRowCommand="{Binding ValidateRowCommand}" />
        </dxmvvm:Interaction.Behaviors>
    </dxg:GridControl>
    
    [DevExpress.Mvvm.DataAnnotations.Command]
    public void ValidateRow(DevExpress.Mvvm.Xpf.EditFormRowValidationArgs args) {
        var context = ((EditIssueInfo)args.Tag).Context;
        context.SaveChanges();
    }
    
  6. Users can remove rows from the Data Grid. To do that, they select rows and call the DeleteCommand command.

    As a result, the behavior raises the ValidateRowDeletion event and executes the ValidateRowDeletionCommand. Use them to validate rows, check database constraints, and delete rows from the database.

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <dxmvvm:Interaction.Behaviors>
            <dxg:DialogEditFormBehavior x:Name="EditFormBehavior" KeyProperty="Id" 
                    CreateEditItemViewModelCommand="{Binding CreateEditEntityViewModelCommand}" 
                    ValidateRowCommand="{Binding ValidateRowCommand}"
                    ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}" />
        </dxmvvm:Interaction.Behaviors>
    </dxg:GridControl>
    
    [DevExpress.Mvvm.DataAnnotations.Command]
    public void ValidateRowDeletion(DevExpress.Mvvm.Xpf.EditFormDeleteRowsValidationArgs args) {
        var key = (int)args.Keys.Single();
        var item = new Issue() { Id = key };
        var context = new IssuesContext();
        context.Entry(item).State = EntityState.Deleted;
        context.SaveChanges();
    } 
    

    You can also add a KeyBinding to allow users to press Delete to remove selected rows:

    <dxg:GridControl x:Name="grid" ItemsSource="{Binding InstantFeedbackSource}">
        <!-- ... -->
        <dxg:GridControl.InputBindings>
            <KeyBinding Command="{Binding DeleteCommand, ElementName=EditFormBehavior}" Key="Delete" />
        </dxg:GridControl.InputBindings>
    </dxg:GridControl>
    

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.

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