私のプロジェクトの短い構造は次のとおりです(MVVM Light Toolkitを使用):
ビュー (UserControl) と DataGrid
ビューモデル
ValueObjects を使用した DataAccess
DB
私は DataGrid への DataBinding に ObservableCollection を使用していますが、DataGrid の行を削除して DataBase に保存することに行き詰まりました。
以前のプロジェクトでは、System.Windows.Input の CommandManager.PreviewExecuted イベントを使用し、そこで DataGrid.DeleteCommand のイベント引数をチェックしました。単にこれ:
if(e.Command == DataGrid.DeleteCommand)
{
DataAccessContext.Sample.DeleteOnSubmit(data);
DataAccessContext.SubmitChanges();
}
数時間グーグルで検索しましたが、正しい方法がわかりません。PassEventArgsToCommand を使用しようとしましたが、Event DataGrid.DeleteCommand または CommandManager.PreviewExecuted が起動しません。SelectionChangedCommand はうまく機能しますが、重要な DataGrid.DeleteCommand を確認する方法がわかりません。
これが私のxamlです:
<DataGrid x:Name="dataGrid1" ItemsSource="{Binding Items}" CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="False" Height="391" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" Margin="2,0,0,0" RowEditEnding="dataGrid1_RowEditEnding" CellEditEnding="dataGrid1_CellEditEnding">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding SelectionChangedCommand,Mode=OneWay}"
CommandParameter="{Binding SelectedItems,ElementName=dataGrid1}">
</cmd:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="DataGrid.DeleteCommand"> //I've also tried PreviewExecuted and CommandManager.PreviewExecuted as EventName
<cmd:EventToCommand
Command="{Binding SelectedItems,Mode=OneWay}"
CommandParameter="{Binding ExecutedRoutedEventArgs, ElementName=dataGrid1}"
PassEventArgsToCommand="True"
></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="auto"></DataGridTextColumn>
<DataGridTextColumn Header="Vorname" Binding="{Binding Surname, UpdateSourceTrigger=PropertyChanged}" Width="auto"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
ご回答ありがとうございます。