Answer the question
In order to leave comments, you need to log in
How to fix nullreferenceexception in a property?
There is a property in the window
public FormulaViewModel ViewModel
{
get
{
return DataContext as FormulaViewModel ?? (DataContext = new FormulaViewModel()) as FormulaViewModel;
}
set
{
DataContext = value;
}
}
private void Parameters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
ViewModel.Parameters.Add(e.NewItems[0] as Parameter);
break;
}
case NotifyCollectionChangedAction.Remove:
{
ViewModel.Parameters.Remove(e.OldItems[0] as Parameter);
break;
}
}
}
Answer the question
In order to leave comments, you need to log in
For God's sake, do not sculpt everything in one line. It doesn't add to the readability. And the problem is most likely just because of this!
if (DataContext is null)
DataContext = new FormulaViewModel()
return DataContext;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question