4est
4est

Reputation: 3168

Access to the property from MainViewModel (WPF, C#)

I have class FrontModel contains some fields from XAML:

public class FrontModel()
{
  public static string LoginName { get; set;}
  public static string userPass;
  public static string Domain { get; set;}
}

Into ViewModel I'm try to connect to FrontModel

public class MainViewModel
{
  FrontModel fm = new FrontModel();

  public MainViewModel()
  {
   ...
   fm.LoginName = Environment.UserName.ToString();//error
  }  
}

But I don't have access to my field. What I'm doing here wrong?

enter image description here

I know that LoginName {get; set;} can do directly into MainViewModel and then it's working, but I'm trying to move it to separate class.

Upvotes: 0

Views: 62

Answers (1)

Alfie
Alfie

Reputation: 2013

This is because you are referencing a static property on a non static instance of FrontModel. Try:

FrontModel.LoginName = Environment.UserName.ToString();

Or if the property does not need to be static, remove static.

Upvotes: 2

Related Questions