全部,
我有一个 DataGrid,它的 CurrentItem 属性绑定到我的 VM 中的某个属性。我还有一个 ICommand 创建一个新对象,将其添加到 DataGrid 的 ItemSource 绑定到的集合中,并将 CurrentItem 设置为等于新对象。
一切都很好,除了由于某种原因在通过代码更改 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");
}
}
/*...*/
}