Josh
Josh

Reputation: 10604

WPF Datagrid RowEditEnding in an MVVM environment

I've been trying to capture the values that a user enters into a datagrid by using

<b:Interaction.Triggers>
    <b:EventTrigger EventName="RowEditEnding">
        <b:InvokeCommandAction  Command="{Binding ReleaseRowEditEndingCommand}" CommandParameter="{Binding SelectedRelease}"/>
    </b:EventTrigger>

But this doesn't work and I now understand this afte reading this article on StackOverflow. The solutions presented all seem to be based on directly calling a method signature that matches the event being raised, in this case

private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)

Has anyone accomplished getting the values post-rowedit in an MVVM situation? All the solutions seem to tightly bind the event to the XAML and I'd like to avoid that, if possible.

Upvotes: 7

Views: 5935

Answers (2)

Josh
Josh

Reputation: 10604

The solution was actually easier than I thought. I changed the XAML and I can now get the values in the RowEditEnding event on the View Model. Here's the before on the data columns on the datagrid:

 <DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Is Paid" Binding="{Binding IsPaid, Mode=TwoWay}" />
    <DataGridTextColumn Header="Amount" Binding="{Binding Amount, Mode=TwoWay}" />
</DataGrid.Columns>

After

 <DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Is Paid" Binding="{Binding IsPaid, UpdateSourceTrigger=PropertyChanged}" />
    <DataGridTextColumn Header="Amount" Binding="{Binding Amount,  UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>

Upvotes: 2

Barracoder
Barracoder

Reputation: 3764

You could try wrapping your data in a ListCollectionView and binding the DataGrid to the ListCollectionView. Then, in your ViewModel, hook into the ListCollectionView.CurrentChanging event to process your changes before moving to the new row.

Upvotes: 0

Related Questions