我设计了一个视图,用于从Datagrid
UI获取用户想要的输入。
添加(+)
按钮更好用意味着它一个接一个地添加行但是当我按下删除(X)
按钮时它会抛出一个异常。其实我想通过单击删除按钮删除特定行。我怎么解决?
XMAL和GridView:
这是代码
public class SaleViewModel : ViewModelBase, INotifyPropertyChanged
{
private ViewModelLocator _locator;
public SaleViewModel():base()
{
_locator = new ViewModelLocator();
_myObject = new ObservableCollection<Sale>();
_Item = new Sale();
Items = new ObservableCollection<Sale>();
Items.Add(new Sale());
}
private RelayCommand _createNewRowCommand;
public RelayCommand CreateNewRow
{
get { return _createNewRowCommand ?? (_createNewRowCommand = new RelayCommand(() => createNewRow())); }
}
void createNewRow()
{
Items.Add(new Sale());
}
private RelayCommand<Sale> _deleteRowCommand;
public RelayCommand<Sale> DeleteRow
{
get { return _deleteRowCommand ?? (_deleteRowCommand = new RelayCommand<Sale>((p) => deleteRow(p))); }
}
void deleteRow(Sale p)
{
Items.Remove(p);
Items = new ObservableCollection<Sale>(Items);
}
private RelayCommand _SaveItemCommand;
public RelayCommand SaveItem
{
get { return _SaveItemCommand ?? (_SaveItemCommand = new RelayCommand(() => saveItem())); }
}
TestEntities3 context;
void saveItem()
{
context = new TestEntities3(); ;
context.Sales.Add(Item);
context.SaveChanges();
}
private ObservableCollection<Sale> _myObject;
public ObservableCollection<Sale> Items
{
get { return _myObject; }
set
{
_myObject = value;
base.RaisePropertyChanged("Items");
}
}
private Sale _Item;
public Sale Item
{
get { return _Item; }
set { _Item = value; base.RaisePropertyChanged("Item"); }
}
public int? Qty
{
get { return _Item.Qty; }
set { _Item.Qty = value; base.RaisePropertyChanged("Qty"); }
}
}
请告诉我我在哪里做错了?
答案 0 :(得分:0)
试试这个
if(p!=null)
{
Items.Remove(p);
.......
}