DataGridRowEditEndingEventArgs 的 row.item 返回 null
DataGridRowEditEndingEventArgs 's row.item returning null
我刚刚创建了一个新的 WPF MVVMLight 项目,很简单:
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>
主要(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()
{
}
}
人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;
}
}
尝试添加新行时,在 RowEditEnding
中,e.Row.item
显示 null :
为什么会这样?
注意:我在 Peronnes
列表中尝试了 ObservableColletion 和 BindingList,在 DataGrid 的 ItemsSource 的绑定中尝试了 PropertyChanged 和 LostFocus
中的 UpdateSourceTrigger
,总是结果相同!
看来您需要手动指定列并精确 UpdateSourceTrigger
,这应该可以正常工作!
<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>
...
因此设置 AutoGenerateColumns="False"
无助于实现结果!
我刚刚创建了一个新的 WPF MVVMLight 项目,很简单:
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>
主要(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()
{
}
}
人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;
}
}
尝试添加新行时,在 RowEditEnding
中,e.Row.item
显示 null :
为什么会这样?
注意:我在 Peronnes
列表中尝试了 ObservableColletion 和 BindingList,在 DataGrid 的 ItemsSource 的绑定中尝试了 PropertyChanged 和 LostFocus
中的 UpdateSourceTrigger
,总是结果相同!
看来您需要手动指定列并精确 UpdateSourceTrigger
,这应该可以正常工作!
<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>
...
因此设置 AutoGenerateColumns="False"
无助于实现结果!