Reputation: 8301
I just created a new WPF MVVMLight Project, simple :
MainWindow.xaml
<Window x:Class="DG.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<command:EventToCommand Command="{Binding Loaded}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<DataGrid ItemsSource="{Binding Personnes, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True" CanUserAddRows="True" CanUserDeleteRows="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="RowEditEnding">
<command:EventToCommand Command="{Binding RowEditEnding}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</Grid>
</Window>
Main (the ViewModel)
public class MainViewModel : ViewModelBase
{
public RelayCommand Loaded
{
get
{
return new RelayCommand(() =>
{
Personnes = new ObservableCollection<Personne>();
Personnes.Add(new Personne("Daoudi", "25"));
Personnes.Add(new Personne("Aymen", "26"));
});
}
}
public RelayCommand<DataGridRowEditEndingEventArgs> RowEditEnding
{
get
{
return new RelayCommand<DataGridRowEditEndingEventArgs>((e) =>
{
int i = 5;
});
}
}
public ObservableCollection<Personne> Personnes
{
get { return _personnes; }
set
{
_personnes = value;
RaisePropertyChanged("Personnes");
}
}
private ObservableCollection<Personne> _personnes;
public MainViewModel()
{
}
}
Personne class
public class Personne
{
public string Name { get; set; }
public string Age { get; set; }
public Personne ()
{
}
public Personne(string name, string age)
{
Name = name;
Age = age;
}
}
When trying to add a new row, in the RowEditEnding
, the e.Row.item
shows null :
Why is this happening ?
Note : I tried both ObservableColletion and BindingList for the Peronnes
list, and tried both PropertyChanged and LostFocus
for the UpdateSourceTrigger
in the binding of the ItemsSource of the DataGrid, always same result !
Upvotes: 0
Views: 747
Reputation: 8301
It seems that you need to specify the columns manually and precise the UpdateSourceTrigger
, this should work normally !
<DataGrid HorizontalAlignment="Left" Margin="0,10,0,0" AutoGenerateColumns="False" ItemsSource="{Binding Personnes}" VerticalAlignment="Top" Height="148" Width="282"
CanUserAddRows="True" CanUserDeleteRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name, UpdateSourceTrigger=LostFocus}"/>
<DataGridTextColumn Header="Age" Binding="{Binding Age, UpdateSourceTrigger=LostFocus}"/>
</DataGrid.Columns>
...
So setting the AutoGenerateColumns="False"
won't help achieve the result !
Upvotes: 1