我在MVVM一個新手,我有一個Model.cs其中包含的一些屬性‘ID’和‘PositionName’我得到了View.cs這包含點擊我在「Items.PositionName == NULL」遇到NullReference錯誤後SelectedItems = {綁定表項}和的ItemSource = {裝訂位置}的DataGrid,並用命令= {結合SHowEdits}按鈕。
這是我的代碼。C# - System.NullReferenceException「類型的未處理的異常'
ViewModel.cs
class PositionVM : INotifyPropertyChanged
{
private ObservableCollection<PositionModel> _position;
private PositionModel _items;
private ICommand _showedits;
public ObservableCollection<PositionModel> Position
{
get
{
return _position;
}
set
{
_position = value;
NotifyProperty("Position");
}
}
public PositionModel Items
{
get
{
return _items;
}
set
{
_items = value;
NotifyProperty("Items");
}
}
public ICommand ShowEdits
{
get
{
if (_showedits == null)
_showedits = new ShowEdit();
return _showedits;
}
set
{
_showedits = value;
}
}
public PositionVM()
{
Position = new ObservableCollection<PositionModel>();
Position.Add(new PositionModel()
{
ID = 1,
PositionName = "asd"
});
}
public void ShowEditDialog()
{
if (Items.PositionName == null)
{
MessageBox.Show("ERROR");
}
else
{
PositionView view = new PositionView();
Data.ID = view.txtid.Text;
var z = new PositionView();
z.ShowDialog();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyProperty(String info)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
爲什麼會出現這個錯誤?我如何避免它? Thanksss