ListBox の SelectedItem プロパティまたは DataGrid の CurrentItem プロパティを使用して問題に対処し、それを ViewModel のプロパティにバインドすることを好みます。
<ListBox ItemsSource="{Binding HarvestTimeSheet}" SelectedItem={Binding CurrentEntry}.../>
<Button Content="Done Editing..." Command="{Binding DoneEditingCommand}"/>
私のViewModelには
private Harvest_TimeSheetEntry _currentHarvest_TimeSheetEntry;
public Harvest_TimeSheetEntry CurrentHarvest_TimeSheetEntry
{
get { return _currentHarvest_TimeSheetEntry; }
set
{
if (_currentHarvest_TimeSheetEntry == value) return;
_currentHarvest_TimeSheetEntry = value;
RaisePropertyChanged("CurrentHarvest_TimeSheetEntry");
}
}
これは、ListBox で選択された項目に設定されます。
私のViewModelはボタンのコードを提供します。RelayCommand と RaisePropertyChanged を簡単に提供するために、MVVM ライトを使用しています。
private RelayCommand _doneEditingCommand;
public RelayCommand DoneEditingCommand
{
get { return _doneEditingCommand ?? (_doneEditingCommand = new RelayCommand(HandleDoneEditing, () => true)); }
set { _doneEditingCommand = value; }
}
public void HandleDoneEditing()
{
if (CurrentHarvest_TimeSheetEntry != null)
//Do whatever you need to do.
}
少し手間がかかりますが、フローを非常に柔軟に制御できます。