Reputation: 763
I am creating a project using WPF and MVVM-Light library from GalaSoft. I will have a base abstract View Model class, which will be used by all other View Model classes implemented. There I will have MVVM-Light base class as my base class. However, inside this base class, when I try to use RaisePropertyChanged function I get the following error:
An object reference is required for the non-static field, method, or property 'GalaSoft.MvvmLight.ViewModelBase.RaisePropertyChanged(string)'
The code will look like this:
AnalysisViewModelBase : ViewModelBase
{
public const string TagDescriptionStringListPropertyName = "TagDescriptionStringList";
protected static List<string> m_tagDescriptionStringList;
public static List<string> TagDescriptionStringList
{
get
{ return m_tagDescriptionStringList; }
set
{
if (m_tagDescriptionStringList == value)
return;
m_tagDescriptionStringList = value;
RaisePropertyChanged(TagDescriptionStringListPropertyName);
}
}
protected AnalysisViewModelBase()
{
m_tagDescriptionStringList = new List<string>();
m_tagDescriptionStringList.AddRange(new string[] { "North Position", "East Position", "Depth" });
}
}
AnotherViewModel : AnalysisViewModelBase
{ ... }
Could anyone please help me understand what is wrong with my RaiseProperyChanged function?
Upvotes: 0
Views: 2814
Reputation:
You simply must declare your property "Tax DescriptionStringList " as non static. Since the backingfield (m_tagDescriptionStringList)
is static it remains the same thing. Make this :
class AnalysisViewModelBase : ViewModelBase
{
public const string TagDescriptionStringListPropertyName = "TagDescriptionStringList";
protected static List<string> m_tagDescriptionStringList;
public List<string> TagDescriptionStringList
{
get
{ return m_tagDescriptionStringList; }
set
{
if (m_tagDescriptionStringList == value)
return;
m_tagDescriptionStringList = value;
RaisePropertyChanged(TagDescriptionStringListPropertyName);
}
}
protected AnalysisViewModelBase()
{
m_tagDescriptionStringList = new List<string>();
m_tagDescriptionStringList.AddRange(new string[] { "North Position", "East Position", "Depth" });
}
}
AnotherViewModel : AnalysisViewModelBase
{ ... }
If it is absolutely necessary to keep the property as a static property in this case, here is a solution: raise the property changes (using RaisePropertyChanged("TagDescriptionStringList")) when it happens, as I indicated in the code below
class AnalysisViewModelBase : ViewModelBase
{
public const string TagDescriptionStringListPropertyName = "TagDescriptionStringList";
protected static List<string> m_tagDescriptionStringList;
public static List<string> TagDescriptionStringList
{
get
{ return m_tagDescriptionStringList; }
set
{
if (m_tagDescriptionStringList != value)
{
m_tagDescriptionStringList = value;
}
}
}
protected AnalysisViewModelBase()
{
m_tagDescriptionStringList = new List<string>();
m_tagDescriptionStringList.AddRange(new string[] { "North Position", "East Position", "Depth" });
RaisePropertyChanged("TagDescriptionStringList");
}
}
AnotherViewModel : AnalysisViewModelBase
{ ... }
Upvotes: 0
Reputation: 1552
You are trying to access a Non Static Method From a Static Method... It does not have access to this value, you have to make your method non static.
here is a web page which explains about static methods if you want to have a better understanding of why you can't do what you are trying to do.
Upvotes: 0