Search code examples
wpfmvvm-lightwpfdatagrid

Fire a DataGrid Cell Edit Event with MVVM Design


I am working on a WPF datagrid and MVVM design using Galasoft. Couple of columns are editable and I need to perform a database call during cell edit.

I am trying to capture the event during the cell edit. I am able to do this using EventToCommand option in Galsoft. But EventToCommand is applied at grid level and the event is getting fired for every cell click event. I dont want to happen this.

<DataGrid Grid.Column="0" Grid.Row="1" CanUserResizeRows="False" AutoGenerateColumns="False" x:Name="AdvisorDataGrid" HorizontalScrollBarVisibility="Auto" 
                                        ScrollViewer.VerticalScrollBarVisibility="Auto" IsReadOnly="False" RowHeaderWidth="0" 
                                        Background="White" CanUserSortColumns="True" GridLinesVisibility="All" IsTabStop="False" 
                                         ItemsSource="{Binding MemberAdvisorsList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"
                                         IsEnabled="{Binding IsGridAdvisorsEnabled}" CanUserAddRows="True" EnableRowVirtualization="True"
                                   CurrentCellChanged="DataGrid_CurrentCellChanged"  SelectedItem="{Binding SelectedMemberDetails,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="OnFocus">
                                    <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding NavigateToArticleCommand,Mode=OneWay}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>

Also, to prevent every cell event, I moved the EventToCommand to the column level, but then the event is not getting fired.

Is there anyway, I can do this? Also, what's the best event name to capture the cell edit completed action? I am currently using LostFocus, but this is getting fired even my first time click.

Any help is highly appreciated.

Thanks


Solution

  • I ended up adding a condition check to filter the column which I need to perform CellEditEnding action.

    Basically, OnCellEditEnding event, I check whether the Column.Header.equals("Column1") then perform the action.

    By default, this event will occur for all cells.