xaml file:
<DataGrid x:Name="BrugerDataGrid" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" IsReadOnly="False" SelectionMode="Single" SelectionUnit="FullRow" ItemsSource="{Binding Brugere, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="RowEditEnding">
<i:InvokeCommandAction Command="{Binding RowEditEndingCommand}" CommandParameter="{Binding SelectedItem, ElementName=BrugerDataGrid}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<!-- DataGrid columns -->
viewmodel:
public ObservableCollection<Bruger> Brugere { get; set; }
public ICommand RowEditEndingCommand => new RelayCommand<Object>(OnRowEditEnding);
private void OnRowEditEnding(Bruger bruger)
{
// bruger is autogenerated from entity framework
}
the bruger i am reciving is the value before data is changed in the grid. how can i get the value after i have changed it , so that i can store the changed data to my database
I have been trying to add an event on this methos,
private void OnRowEditEnding(Bruger bruger,DataGridRowEditEndingEventArgs e)
but that will give me an error on this line: public ICommand RowEditEndingCommand => new RelayCommand(OnRowEditEnding); Saying: Argument 1: cannot convert from 'method group' to 'System.Action<Admin.DBAccess.Models.Bruger?>'
i thought PassEventArgsToCommand="True" maybe would allow me to that.
The RowEditEnding
event occurs before a row edit is committed or canceled.
What you should do is to implement the IEditableObject interface in your Bruger
class and handle the logic in the EndEdit()
method.
The other option would be to explicitly commit the edit in an event handler in the view:
private bool _handle = true;
private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
if (_handle)
{
_handle = false;
BrugerDataGrid.CommitEdit();
var updatedItem = e.Row.Item as Bruger;
var viewModel = this.DataContext as YourViewModel;
if (updatedItem != null && viewModel != null)
viewModel.RowEditEndingCommand.Execute(updatedItem);
_handle = true;
}
}
Passing event args to a view model breaks the MVVM design pattern. Invoking a command programmatically in the code-behind of a view does not.