全て、
VM のプロパティにバインドされた CurrentItem プロパティを持つ DataGrid があります。また、新しいオブジェクトを作成し、それを DataGrid の ItemSource がバインドされているコレクションに追加し、CurrentItem を新しいオブジェクトに等しく設定する ICommand もあります。
コードを介して CurrentItem バインディングを変更するときに何らかの理由でコミットが呼び出されないことを除いて、すべてがうまく機能しています。
以下のコードの関連部分を参照してください。
XAML:
<DataGrid ItemSource={Binding} CurrentItem={Binding Path=CurrentItem, UpdateSourceTrigger=PropertyChange, Source={StaticResource VM}}>
<DataGrid.InputBindings>
<KeyBinding Command={Binding Path=AddNewItemCommand, UpdateSourceTrigger=PropertyChanged, Source={StaticResource VM}} Key="OemPlus" Modifiers="Control" />
</DataGrid.InputBindings>
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
仮想マシン:
Class cVM:INotifyPropertyChanged
{
/*...RaisePropertyChanged(string str) method implimented here to handle PropertyChanged event*/
private ICommand _AddNewItemCommand; //defined in Constructor. Adds new item to Collection and sets CurrentItem property.
ICommand AddNewItemCommand{ get { return _AddNewItemCommand; } }
private object _CurrentItem;
public object CurrentItem
{
get
{
return _CurrentItem;
}
set
{
_CurrentItem = value;
RaisePropertyChanged("CurrentItem");
}
}
/*...*/
}